Need a matlab code for calling rows in MATLAB

2 views (last 30 days)
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.)

Answers (3)

Image Analyst
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

Youssef  Khmou
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
shailesh khaire
shailesh khaire on 1 Mar 2013
Thanks a lot for the reply sir. And it works as well. But...i apologize that i did not explain the problem precisely. Your code indeed works...But it gives me an answer only for first two rows! but i want to create a code such that it will take only 3 number of rows each time to calculate. That means...For the first time it should take 1st 3 rows...then (rows 4,5 and 6...) then...7,8,9...and go on...till the last row of matrix.
Finally...that code would create a matrix of all those rows.
Thanks a ton for your precious help.
Youssef  Khmou
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

Sign in to comment.


Jan
Jan on 1 Mar 2013
D = rand(3000, 4);
E = sum(reshape(D, 3, 1000, 4)) / 3;
E = reshape(E, 1000, 4);
  1 Comment
shailesh khaire
shailesh khaire on 1 Mar 2013
Thanks a lot for the reply sir. And it is producing a long matrix...But that is not what i am expecting. I apologize that i did not explain the problem precisely. I have a matrix D (768 x 3). i want to create a code such that it will take only 3 number of rows each time to operate on. it would give me the matrix of the final (256 x 3) matrix. That means...For the first time it should take 1st 3 rows...then (rows 4,5 and 6...) then...7,8,9...and go on...till the last row of matrix.
Finally...that code would create a matrix of all those rows.
Thanks a ton for your precious help.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!