Adding a second for loop to create different matrices

2 ビュー (過去 30 日間)
Inti Vanmechelen
Inti Vanmechelen 2015 年 12 月 22 日
編集済み: Mohammad Abouali 2015 年 12 月 22 日
I have this for loop (see below), and I want to create 10 empty 6x3 matrices (S1 to S10). Is there a way to add a second loop that loops over 1 to 10 so I don't have to repeat everything 10 times?
names2 = {'soleus' 'tibant' 'gaslat' 'vaslat' 'rectfem' 'hamlat'};
Vars2 = {soleus, tibant, gaslat, vaslat, rectfem, hamlat};
nvars = length(Vars2);
for s = 1:1:nvars
S1.(names2{s}) = zeros(100,3);
S2.(names2{s}) = zeros(100,3);
S3.(names2{s}) = zeros(100,3);
S4.(names2{s}) = zeros(100,3);
S5.(names2{s}) = zeros(100,3);
S6.(names2{s}) = zeros(100,3);
S7.(names2{s}) = zeros(100,3);
S8.(names2{s}) = zeros(100,3);
S9.(names2{s}) = zeros(100,3);
S10.(names2{s}) = zeros(100,3);
end
  1 件のコメント
Stephen23
Stephen23 2015 年 12 月 22 日
Although beginners always ignore this advice, here it is anyway: Do NOT define variable names dynamically. Read my answer below to know why this is a bad programming practice.

サインインしてコメントする。

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 12 月 22 日
編集済み: Mohammad Abouali 2015 年 12 月 22 日
Yes, there is a way; but (really really) not recommended: (why not, refer to Stephens answer) But if you insist, here is how you can do it (but again you shouldn't do it)
for s = 1:1:nvars
for idx=1:10
eval(sprintf('S%d.(name2{s}) = zeros(100,3);',idx))
end
end
You better off using array of structure
for s = 1:1:nvars
for idx=1:10
S(idx).(names2{s}) = zeros(100,3);
end
end
  2 件のコメント
Inti Vanmechelen
Inti Vanmechelen 2015 年 12 月 22 日
Thank you, I'm using the structure input now.
I wanted to do something similar as you posted above, but it's not working.. I'm trying to create 10 empty 6x3 matrices and I tried this imput:
for idx = 1:10
subject(idx) = zeros(6,3);
end
If I try to run it, matlab says "Subscripted assignment dimension mismatch"
Mohammad Abouali
Mohammad Abouali 2015 年 12 月 22 日
編集済み: Mohammad Abouali 2015 年 12 月 22 日
You are correct. That wouldn't work because:
subject(idx) = zeros(6,3);
will treat subject as a regular matrix therefore, subject(id) should have only one element while zeros(6,3) is a matrix.
The alternative is to use cell arrays as follow:
subject=cell(10,1);
for idx = 1:10
subject{idx} = zeros(6,3);
end
Depending on what you want to do, you might not need cell or structure at all. Depending what your calculations are may be this would work too:
subject = zeros(6,3,10);
This is a three-dimensional array with third dimension being your 10 matrices. The data structure that you use is related to what type of calculation you want to do.

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 12 月 22 日
編集済み: Stephen23 2015 年 12 月 22 日
Solution
S(1).(names2{s}) = zeros(100,3);
S(2).(names2{s}) = zeros(100,3);
S(3).(names2{s}) = zeros(100,3);
... etc
Obviously you can loop over the indices of the structure. Or you can learn how to write efficient MATLAB code without loops: this allocates all those zeros-arrays to all fields of a 1x10 structure, in just three lines:
>> names2 = {'soleus','tibant','gaslat','vaslat','rectfem','hamlat'};
>> X(1,:) = names2;
>> X(2,:) = {zeros(100,3)};
>> S = repmat(struct(X{:}),1,10);
Why waste your time writing loops when you can do that instead?
And whatever you do, do not define those variable names S1, S2, etc, dynamically.
Why Dynamic Variable Names are a BAD IDEA
Avoid creating dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use:
There are many functions that support working on structures and cell arrays and can access their data easily, and many functions operate on complete numeric arrays all at once without any loops (i.e. vectorized code, which is something you need to learn about).
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:
Here is a discussion of why it is a bad idea to include meta-data (such as an index) in a variable name:
And here is a discussion of why it is a really bad idea to make variables magically appear any workspace (even though beginners love doing this):
And in case you thought this is a MATLAB restriction, here is the same discussion for some other languages, advising "DO NOT create dynamic variable names":

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by