Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

using structures and for loops

1 visualizzazione (ultimi 30 giorni)
Inti Vanmechelen
Inti Vanmechelen il 22 Dic 2015
Chiuso: MATLAB Answer Bot il 20 Ago 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.

Risposte (1)

Guillaume
Guillaume il 22 Dic 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.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by