Need a matlab code for calling rows in MATLAB
2 views (last 30 days)
Show older comments
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 Comments
Answers (3)
Image Analyst
on 1 Mar 2013
Edited: Image Analyst
on 1 Mar 2013
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 Comments
Youssef Khmou
on 1 Mar 2013
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 Comments
Youssef Khmou
on 1 Mar 2013
Edited: Youssef Khmou
on 1 Mar 2013
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
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!