Borrar filtros
Borrar filtros

How to efficiently create a vector?

2 visualizaciones (últimos 30 días)
balandong
balandong el 23 de Jun. de 2017
Comentada: Star Strider el 23 de Jun. de 2017
I want create a matrix such that
A matrix mat_B = [ 1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20].
Is there any other code that more efficient than the code I make below?
for i = 1:5 if i ==1 a (i,:)=1:4; else a (i,:) = (a (i-1,:))+4; end end

Respuesta aceptada

Star Strider
Star Strider el 23 de Jun. de 2017
I would use the reshape function to avoid the explicit loops:
v = 1:20;
mat_B = reshape(v, 5, [])';
  2 comentarios
balandong
balandong el 23 de Jun. de 2017
Yes, I agree with you, this indeed a more compact solution. Thanks
Star Strider
Star Strider el 23 de Jun. de 2017
My pleasure.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by