How to create a sequence.

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

 Risposta accettata

Sean de Wolski
Sean de Wolski il 23 Lug 2014
v = 2.^(1:8).'

5 Commenti

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
for ii = 10
v = 2.^(1:ii).'
end
Stephen23
Stephen23 il 5 Ago 2016
Modificato: Stephen23 il 5 Ago 2016
@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.
Thanks a lot!
Simon Siu
Simon Siu il 5 Feb 2020
how to you know that!!!!!!!!!!!!!!!?????

Accedi per commentare.

Più risposte (1)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by