I need to create a vector that holds a sequence. In the sequence the previous number is multiplied by 2. The sequence could stop at a desired number lets say 256 (but it could be a bigger number). How would one go about this?
Example sequnce = 2,4,8,16,32,...,256. vector = [2;4;8;16;32;64;128;256]
vector =
2
4
8
16
32
64
128
256

 採用された回答

Sean de Wolski
Sean de Wolski 2014 年 7 月 23 日

0 投票

v = 2.^(1:8).'

5 件のコメント

Sandie Nhatien Vu
Sandie Nhatien Vu 2016 年 8 月 5 日
Hi, i have a question.. How do i create that sequence using a for-loop? It should range:
2 4 8 16 32 64 128 256 512 1024 that is i = 1:10
Sean de Wolski
Sean de Wolski 2016 年 8 月 5 日
for ii = 10
v = 2.^(1:ii).'
end
Stephen23
Stephen23 2016 年 8 月 5 日
編集済み: Stephen23 2016 年 8 月 5 日
@Sean de Wolski: that made me choke on my water with laughter... it really does fulfill the requirement perfectly, this wins the "best intentional misuse of a for-loop" prize.
Sandie Nhatien Vu
Sandie Nhatien Vu 2016 年 8 月 5 日
Thanks a lot!
Simon Siu
Simon Siu 2020 年 2 月 5 日
how to you know that!!!!!!!!!!!!!!!?????

サインインしてコメントする。

その他の回答 (1 件)

Morm My
Morm My 2021 年 7 月 29 日

0 投票

thank you very much sir.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 7 月 23 日

回答済み:

2021 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by