How to do loop in the Workspace?
Show older comments
I have four matrixes c1 c2 c3 c4 (each matrix is 45x9)in the workspace. I need to calculate the mean for each row in each matrix.
The code I tried does not work:
c = {c1, c2, c3, c4};
for j = 1:4
mean_c(j) = mean(c{j},2);
end
Accepted Answer
More Answers (2)
Andrei Bobrov
on 20 Sep 2016
Edited: Andrei Bobrov
on 20 Sep 2016
C = cat(1,c1,c2,c3,c4);
d = reshape(mean(C,2),size(c1,1),[]);
or
C = cat(1,c{:});
d = reshape(mean(C,2),size(c1,1),[]);
clc;close all;clear all;
% Take some random data for c1,c2,c3,c4
c1 = rand(45,9) ;
c2 = rand(45,9) ;
c3 = rand(45,9) ;
c4 = rand(45,9) ;
c{1} = c1 ;
c{2} = c2 ;
c{3} = c3 ;
c{4} = c4 ;
mean_c = zeros(45,4) ; % initialize the mean for each ci
for j = 1:4
mean_c(:,j) = mean(c{j},2);
end
Categories
Find more on Data Types 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!