Need a matlab code for calling rows in MATLAB
조회 수: 3 (최근 30일)
이전 댓글 표시
I have matrix
D = [...
8.5000 0.6000 92.1000 5.3420
8.5000 0.6000 92.0000 5.4390
8.5000 0.6000 92.2000 5.3520
8.4000 0.6000 92.1000 5.4490
8.5000 0.6000 92.1000 5.3620
8.5000 0.6000 92.1000 5.4590
8.5000 0.6000 92.2000 5.3720
8.5000 0.6000 92.1000 5.4690
8.5000 0.6000 92.1000 5.3820]
Now i want to write a matlab code such that it will take first 3 rows. e.g. in this case -
8.5000 0.6000 92.1000 5.3420
8.5000 0.6000 92.0000 5.4390
8.5000 0.6000 92.2000 5.3520
then calculate the mean of each column. e.g. for 4th column - (5.342+5.439+5.350/3)= 5.377 and this will continue till n number columns. (n is multiple of 3 assumed.)
댓글 수: 0
답변 (3개)
Image Analyst
2013년 3월 1일
편집: Image Analyst
2013년 3월 1일
Try this:
theColumnMeans = mean(D(1:3,:))
theColumnMeans =
8.5000 0.6000 92.1000 5.3777
I'm not sure what "this will continue till n number columns" means though. Please explain that part. Perhaps you meant "this will continue with every groups of 3 rows until the last row in the matrix", in which case you can try this:
theColumnMeans = mean(D(1:3,:))
theColumnMeans = mean(D(4:6,:))
theColumnMeans = mean(D(7:9,:))
% Alternate way using blockproc():
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:));
blockSize = [3 1];
blockyImage = blockproc(D, blockSize, meanFilterFunction)
In the command window:
theColumnMeans =
8.5000 0.6000 92.1000 5.3777
theColumnMeans =
8.4667 0.6000 92.1000 5.4233
theColumnMeans =
8.5000 0.6000 92.1333 5.4077
blockyImage =
8.5000 0.6000 92.1000 5.3777
8.4667 0.6000 92.1000 5.4233
8.5000 0.6000 92.1333 5.4077
댓글 수: 0
Youssef Khmou
2013년 3월 1일
hi,
Given your D matrix :
N=3;
D2=D(1:N,:); % select N rows
M=mean(D2) % M is vector that each elements j is the mean of column j in D2
댓글 수: 2
Youssef Khmou
2013년 3월 1일
편집: Youssef Khmou
2013년 3월 1일
hi, good that it works ok you can create a function that does what you described, i tried to write one for you :
function Y=Consecu_3rMeans( X)
% Your input is matrix X
[m,n]=size(X);
Y=zeros(floor(m/3),n);
j=3;
for i=1:3:m-3
Y(floor((i+3)/3),:)=mean(X(i:j,:));
j=j+3;
end
% check if the number of rows is divisible by 3, if not then there are non
% treated rows .
N=mod(m,3);
if N~=0
Last_row=mean(X(m-3+2:end,:));
%concatenation :
Y=[Y;Last_row];
end
Note if the number of rows is not divisible by 3, then the last rows are not treated so you need to compute theirs means and add the values to the output matrix .
an example :
M=rand(20); % 20lines and 20 columns
% So we compute the mean columns wise of lines 1:3, 4:6, 7:9, %10:12,13:15,16:18 BUT 19:20 are included in the function code .
y=Consecu_3rMeans(X);
%y is of size 7x20 such that each row is mean of i:i+3 rows of input matrix , and 7 not 6 because we added a line that inculdes E[19:20] .
i hope this helps
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!