need help making a 'for' loop to make a new matrix
2 次查看(过去 30 天)
显示 更早的评论
hi
i have 40 matrixes 13x16
i want to make a new matrix 1x40 with max values
im using this:
for iii=1:40
M(1,iii)=max(max(['m-' num2str(iii,'%01d')]));
end
but M values are all 109 (max value of each matrix are less than 1)
im nwe to matlab and i found it on internet and dont know if its right or not
1 个评论
Rik
2021-7-20
You may consider doing the Onramp tutorial (which is provided for free by Mathworks) to get familiar with the Matlab basics.
回答(2 个)
Jonas
2021-7-20
编辑:Jonas
2021-7-20
your loop calculates the maximum value of a character array. since the character 'm' has value 109 (see double('m')) and your character numbers are 48 (double('0')) and 57 (double('9')) all max values are 109.
what do you want to do actually, are there variables in your workspace called m1 to m40? if you created your variables earlier it would be better to save all of them in the same variable, e.g. in the size of 13x16x40
short example how to get the max of each matrix then
a=rand([3 3 3])
max(a,[],[1 2])
2 个评论
Jonas
2021-7-20
to be honest there is a direct possibility. but it uses a method that should be avoided. you can use the eval function for that:
M(1,iii)=max(eval(['m-' num2str(iii,'%01d')]),[],'all');
using eval is bad style and should be avoided. it makes the code unreadable and is most of the case a result of bad data organisation. if you have data that belong to each other, try to save them together and don't use the same variable name with different numbers at the end.
Rik
2021-7-20
Having numbered variables is a problem that should be solved when you create those variables. If you have 40 variables with the same name, that probably means it is actually an array, instead of independent variables. Why not make it an array then? With an array you can use a simple for loop to process each element.
m1=rand(13,16);m2=rand(13,16);m3=rand(13,16);m4=rand(13,16);m5=rand(13,16);m6=rand(13,16);m7=rand(13,16);m8=rand(13,16);m9=rand(13,16);m10=rand(13,16);m11=rand(13,16);m12=rand(13,16);m13=rand(13,16);m14=rand(13,16);m15=rand(13,16);m16=rand(13,16);m17=rand(13,16);m18=rand(13,16);m19=rand(13,16);m20=rand(13,16);m21=rand(13,16);m22=rand(13,16);m23=rand(13,16);m24=rand(13,16);m25=rand(13,16);m26=rand(13,16);m27=rand(13,16);m28=rand(13,16);m29=rand(13,16);m30=rand(13,16);m31=rand(13,16);m32=rand(13,16);m33=rand(13,16);m34=rand(13,16);m35=rand(13,16);m36=rand(13,16);m37=rand(13,16);m38=rand(13,16);m39=rand(13,16);m40=rand(13,16);
%Let's fix the mess first. You should fix it when the variables are
%created.
for n=1:40
m_array{n}=eval(sprintf('m%d',n));
end
%since they are all the same size, we don't actually need a cell aray
m_array=cat(3,m_array{:});
size(m_array)
%now we use max. since a few releases back we can use this syntax:
max_vals=max(m_array,[],[1 2]);
%this is 1x1x40, so let's reshape
max_vals=reshape(max_vals,1,[])
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!