Working with arrays in a for loop

조회 수: 1 (최근 30일)
athpapa -
athpapa - 2011년 1월 28일
I have 2 variables that take their values from a 'for' loop. How can I put the values of those 2 variables into an array? For example, I have the code:
for ....
Ith1 = H - ceil(i./I);
Ith2 = I;
end
and the variables Ith1, Ith2 take some values. I want these values to put them in an array. As a result, when I print the array I want to see all these values!.
e.g. Array with 2 columns (Ith1, Ith2)= 5 10, 2 5, 6 18
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2011년 1월 28일
You can vectorize your code, i.e. calculate your final matrix w/o loops...but you need to give us more details on how you intend to create the first and the second column (criterias)

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

답변 (3개)

Paulo Silva
Paulo Silva 2011년 1월 28일
lth1v=[];lth2v=[];
for ....
Ith1=H-ceil(i./I); Ith2=I;
lth1v=[lth1v lth1];
lth2v=[lth2v lth2];
end
lth=[lth1v lth2v];
If you want or need the code to be fast you should preallocate before the loop
n=? %this is how many time the for will be executed,replace ? by your value
lth1v=zeros(1,n);lth2v=zeros(1,n);

the cyclist
the cyclist 2011년 1월 28일
Something like this is probably what you are after. You might have to play around to get your syntax exactly right, but this should give you an idea.
ltArray = zeros(100,2);
for nrow = 1:100
Ith1=H-ceil(i./I); Ith2=I;
ltArray(nrow,:) = [Ith1,Ith2];
end

athpapa -
athpapa - 2011년 1월 28일
Thank you all for your help!

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by