"for loop" with the out of order inputs

I want to make a "for loop " with the out of order inputs. The simplified code is below.
seq=0;
for i=0.01, 0.1, 1, 8, 100
for j=0.01, 0.1, 1, 8, 100
for seq=seq+1
a(seq)=i+j;
end
end
end
When I run this code I expect to get 25 outputs which uses every terms of "i and j". (0.02, 0,11, 1,01, 8,01, ...) But I get just one output which uses just first inputs of "i and j" (0,02)
How can I handle this" "for loop" with out of order inputs?
Best Regards

 채택된 답변

Cedric
Cedric 2013년 1월 14일
편집: Cedric 2013년 1월 14일

0 개 추천

a = zeros(25,1) ; % Prealloc (optional).
seq = 0 ;
for ii = [0.01, 0.1, 1, 8, 100]
for jj = [0.01, 0.1, 1, 8, 100]
seq = seq+1 ;
a(seq) = ii+jj ;
end
end

추가 답변 (1개)

Matt J
Matt J 2013년 1월 14일

0 개 추천

You don't need a loop.
i=[0.01, 0.1, 1, 8, 100];
j=[0.01, 0.1, 1, 8, 100];
a=reshape(bsxfun(@plus,j(:),i),[],1);
However, the arguments to for-loops must be vectors/matrices
for i=[0.01, 0.1, 1, 8, 100]
etc...

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by