how to change variable name for iteration of vector computation?

3 次查看(过去 30 天)
I am trying to create a loop that changes the variable name in each iteration, basically instead of coding " sec1_mean = mean(vel_sec1); sec2_mean = mean(vel_sec2); sec3_mean = mean(vel_sec3); sec4_mean = mean(vel_sec4); sec5_mean = mean(vel_sec5); sec6_mean = mean(vel_sec6); sec7_mean = mean(vel_sec7); " have a loop like
for i = 1:7
m_name2 = ['vel_sec' num2str(i)]
['sec' num2str(i) '_mean']= mean(m_name2)
end
but I have got error with this loop and the problem is that m_name2's class is char whereas we need it as double so that we can get the mean of say vel_sec1. I would appreciate any help on how I can solve this error.
Thanks

回答(2 个)

Matt J
Matt J 2014-10-20
编辑:Matt J 2014-10-20
You should be holding your vel_sec data as cell array elements vel_sec{i} and do
for i=1:7
sec_mean(i)=vel_sec{i};
end
  2 个评论
maryam
maryam 2014-10-20
Thanks Matt but I think what this do is a little bit different with what I have in mind. here, you are dealing with sec_mean as 1 matrix. I would like to make 7 matrices with the name changing at each loop(eg. sec1_mean, sec2_mean, etc) and the value that is in each matrix is the mean of 7 different matrices that are vel_sec1, vel_sec2, etc...)

请先登录,再进行评论。


Star Strider
Star Strider 2014-10-20
编辑:Star Strider 2014-10-20
I suggest you create it as an array instead. It will be much easier to work with later:
for k1 = 1:7
sec_mean(k1) = mean(eval(sprintf('vel_sec%d',k1)));
end
The mean of all seven matrices is then: mean(sec_mean).
It would be best also to convert the ‘vel_sec#’ vectors to arrays as well, and delete the separate variables. You could use a numeric or cell array for those.
  2 个评论
maryam
maryam 2014-10-20
编辑:maryam 2014-10-20
Thanks Star Strider.. So now I am converting them to arrays, but there is still an error as vel_sec1,... matrices have many rows and 4 columns, and I get this error for eval: eval(sprintf('vel_sec%d',k1)) Index exceeds matrix dimensions. Do you have any idea how I solve this error?
for k1 = 1:7 sec_mean(k1,1:4) = mean(eval(sprintf('vel_sec%d',k1))); end eval(sprintf('vel_sec%d',k1)) Index exceeds matrix dimensions.
Star Strider
Star Strider 2014-10-20
The mean function defaults to taking the means of the columns. If you want to take the means of all the elements of each of the different matrices, this works:
% CREATE DATA
[vel_sec1, vel_sec2, vel_sec3, vel_sec4, vel_sec5, vel_sec6, vel_sec7] = deal(rand(3,4), rand(5,4), rand(7,4), rand(6,4), rand(3,4), rand(5,4),rand(4,4));
for k1 = 1:7
sec_mean(k1) = mean(eval(sprintf('vel_sec%d(:)',k1)));
end
grand_mean = mean(sec_mean);
I included the deal assignment I created to test my code, so you can look at it to be sure it matches your data.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by