Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

using structures and for loops

1 vue (au cours des 30 derniers jours)
Inti Vanmechelen
Inti Vanmechelen le 22 Déc 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
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.

Réponses (1)

Guillaume
Guillaume le 22 Déc 2015
"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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by