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

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 23 de Jul. de 2014

0 votos

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

5 comentarios

Sandie Nhatien Vu
Sandie Nhatien Vu el 5 de Ag. de 2016
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 el 5 de Ag. de 2016
for ii = 10
v = 2.^(1:ii).'
end
Stephen23
Stephen23 el 5 de Ag. de 2016
Editada: Stephen23 el 5 de Ag. de 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.
Sandie Nhatien Vu
Sandie Nhatien Vu el 5 de Ag. de 2016
Thanks a lot!
Simon Siu
Simon Siu el 5 de Feb. de 2020
how to you know that!!!!!!!!!!!!!!!?????

Iniciar sesión para comentar.

Más respuestas (1)

Morm My
Morm My el 29 de Jul. de 2021

0 votos

thank you very much sir.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Jul. de 2014

Respondida:

el 29 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by