Printing in matrix form

9 views (last 30 days)
Donald
Donald on 15 Jun 2011
Hi! I'm trying to neatly display the means of a 3-D matrix, but can't get the results to print as a 2-D matrix.
test = rand(3,3,4)
for i = 1:3; for j = 1:3; grid = mean(test(i,j,:)) end end
When 'grid' prints it lists the nine means individually rather than placing them in a 3x3 matrix. Any suggestions?
Thanks!

Accepted Answer

Sean de Wolski
Sean de Wolski on 15 Jun 2011
No reason for a FOR-loop.
grid = mean(test,3); %second input to mean is the dimension to mean along, in this case 3.
To fix your FOR-loop you just need to keep the indices:
for ii = 1:3; %ii and jj since i,j = sqrt(-1)
for jj = 1:3;
grid(ii,jj) = mean(test(ii,jj,:))
end
end
  1 Comment
Donald
Donald on 15 Jun 2011
that's very simple, thanks!
also, thanks for the head's up

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!