Info

此问题已关闭。 请重新打开它进行编辑或回答。

using structures and for loops

1 次查看(过去 30 天)
Inti Vanmechelen
Inti Vanmechelen 2015-12-22
关闭: MATLAB Answer Bot 2021-8-20
I know I'm supposed to use vectorization instead of for loops, but I have to finish this assignement this way so I hope someone is willing to help me anyway.
This is my code:
names2 = {'soleus' 'tibant' 'gaslat' 'vaslat' 'rectfem' 'hamlat'};
Vars2 = {soleus, tibant, gaslat, vaslat, rectfem, hamlat};
nvars = length(Vars2);
for s = 1:1:nvars
for idx=1:10
P(idx).(names2{s}) = zeros(100,3);
end
end
for k = 1:3
for s = 1:1:nvars
for idx=1:10
P(idx).(names2{s})(:,k)= Vars2{s}.(motion{k})(:,1);
end
end
end
When I open P, it contains the same values for P(1).soleus, P(2).soleus, P(3).soleus, ...P(10).soleus,
while all of them should be different since they are data from different subjects.
'soleus' 'tibant' 'gaslat' 'vaslat' 'rectfem' 'hamlat' are structures containing 3 matrices: Gait(100x10), StairUp(100x10) and StairDown(100x10)
The number of colums (10) equals the number of subjects.

回答(1 个)

Guillaume
Guillaume 2015-12-22
"P contains the same values for P(1).soleus, P(2).soleus, P(3).soleus, ...P(10).soleus". Well, yes it would:
P(idx).(names2{s})(:,k)= Vars2{s}.(motion{k})(:,1);
The left hand side depends on idx, the right hand side does not, so of course, the same value is assigned for all idx. Something on the right must also depends on idx.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by