How to get my for loop working?

조회 수: 1 (최근 30일)
Inti Vanmechelen
Inti Vanmechelen 2015년 12월 22일
편집: Walter Roberson 2015년 12월 22일
Hi,
I want to fill an empty matrix with values that equal the size of other matrices, being muscles. I tried looping over the different muscles, but the loop gives me a matrix with 6 times the size of last muscle, instead of 6 different numbers.
Here's my code:
Names2 = {'soleus', 'tibant', 'gaslat', 'vaslat', 'rectfem', 'hamlat'};
Vars2 = {soleus, tibant, gaslat, vaslat, rectfem, hamlat};
nvars = length(Vars2);
Duration = zeros(6,3);
for s = 1 : nvars
for d = 1:6
Duration(d,1) = length(IndexMuscleActGait.(names2{s}));
end
end
This gives me a matrix with in the first colum 6 times the number 50, which equals length of IndexMuscleActGait of the last muscle. However, I want the length of the 6 different muscles, and I can't find the right command to get it working.

채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 22일
for s = 1 : nvars
Duration(s,1) = length(IndexMuscleActGait.(names2{s}));
end
  댓글 수: 2
Inti Vanmechelen
Inti Vanmechelen 2015년 12월 22일
편집: Walter Roberson 2015년 12월 22일
That was really stupid of me not to think of that.. Thank you!
So no have 3 for loops:
for s = 1 : nvars
Duration2(s,1) = length(IndexMuscleActGait.(names2{s}));
end
for s = 1 : nvars
Duration2(s,2) = length(IndexMuscleActStairUp.(names2{s}));
end
for s = 1 : nvars
Duration2(s,3) = length(IndexMuscleActStairDown.(names2{s}));
end
Would it be possible to get those 3 in one loop by looping over the matrix columns as well? I don't know how to do that, since I'm pulling data from 3 different matrices
Renato Agurto
Renato Agurto 2015년 12월 22일
for s = 1 : nvars
Duration2(s,1) = length(IndexMuscleActGait.(names2{s}));
Duration2(s,2) = length(IndexMuscleActStairUp.(names2{s}));
Duration2(s,3) = length(IndexMuscleActStairDown.(names2{s}));
end
or
for s = 1 : nvars
Duration2(s,1:3) = [length(IndexMuscleActGait.(names2{s})),...
length(IndexMuscleActStairUp.(names2{s})),...
length(IndexMuscleActStairDown.(names2{s}))];
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Renato Agurto
Renato Agurto 2015년 12월 22일
편집: Renato Agurto 2015년 12월 22일
I think you should use
Duration(d,s) = ... %not Duration(d,1)
or maybe what you want is
Duration(d,1) = length(IndexMuscleActGait.(names2{d}));
if this last is correct, why do you have 2 for loops?
  댓글 수: 1
Inti Vanmechelen
Inti Vanmechelen 2015년 12월 22일
That would mean I get 6 columns as well, and I only want 3.
Sorry, I didn't copy enough code for you to understand my full intention. The part I copied above has to give me 6 different numbers in the first column of the matrix.
Then I would repeat the same loop for IndexMuscleActStairUp (instead of Gait) and these 6 six numbers would fill the second column. Then i'd have a third column containing the lengths of IndexMuscleActStairDown.
So the 3 columns of 'Duration' cover the motion, the rows cover the lengths of the IndexMuscleActGait.(names2{s}) matrices.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by