Filter löschen
Filter löschen

how to change variable name for iteration of vector computation?

3 Ansichten (letzte 30 Tage)
maryam
maryam am 20 Okt. 2014
Kommentiert: Star Strider am 20 Okt. 2014
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

Antworten (2)

Matt J
Matt J am 20 Okt. 2014
Bearbeitet: Matt J am 20 Okt. 2014
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 Kommentare
maryam
maryam am 20 Okt. 2014
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...)
Matt J
Matt J am 20 Okt. 2014
Bearbeitet: Matt J am 20 Okt. 2014
Yes, I'm advising you not to do that and so is Star Strider. See also

Melden Sie sich an, um zu kommentieren.


Star Strider
Star Strider am 20 Okt. 2014
Bearbeitet: Star Strider am 20 Okt. 2014
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 Kommentare
maryam
maryam am 20 Okt. 2014
Bearbeitet: maryam am 20 Okt. 2014
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 am 20 Okt. 2014
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by