Help me generating this series of number.

White flag.
I'm trying to make a series of number like below:
1
2
5
10
20
50
100
200
500
...
...
...
1000000000
2000000000
5000000000
but i need a for-end command first
for i=1:20
because i want to use that "20" in other procedure. And the generated numbers willl also be input in another procedure which requires them to be real positive integer.
could you help me?

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 4월 17일

0 개 추천

without loop for
reshape(cumprod([1 2 5;10*ones(9,3)])',[],1);
with loop for
P = zeros(30,1);
for ii = 1:20
if rem(ii,2) == 0,
P(3*(ii/2)-2+(0:2)) = [1;2;5]*10^(ii/2-1);
end
end

댓글 수: 3

Oleg Komarov
Oleg Komarov 2011년 4월 17일
Slightly faster alternatives:
reshape(bsxfun(@times,[1; 2; 5], 10.^(0:9)),[],1);
reshape([1; 2; 5] * 10.^(0:9),[],1);
May I ask?
why do you use
P(3*(ii/2)-2+(0:2))= ...
instead of
P(ii)= ...
?
Andrei Bobrov
Andrei Bobrov 2011년 4월 18일
Because, it is necessary to fulfill your two conditions: length (P) = 30, i = 1:20

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

추가 답변 (1개)

Albaihaqi Albaihaqi
Albaihaqi Albaihaqi 2011년 4월 18일

0 개 추천

Thanks dude for a lot of help... I really need this...
Could I ask you for another problem I might have in the future? Thanks

카테고리

도움말 센터File 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