how to fill matrix without using for loop?

8 visualizaciones (últimos 30 días)
Carlos Oliveira
Carlos Oliveira el 11 de Nov. de 2013
Respondida: Carlos Oliveira el 11 de Nov. de 2013
a=zeros(24,4); b=ones(24,1);
Why does not this work?
a(:,1:end)=b(:)
Subscripted assignment dimension mismatch.
Does anyone know where I am going wrong? bold

Respuesta aceptada

Dale
Dale el 11 de Nov. de 2013
Editada: Dale el 11 de Nov. de 2013
Are you trying to replicate b through A? In which case use the repmat function:
A = repmat(b,24,1)
Edit: Just realised that b is a column, so it should be A = repmat(b,1,4)

Más respuestas (2)

Wayne King
Wayne King el 11 de Nov. de 2013
Editada: Wayne King el 11 de Nov. de 2013
because b isn't the right size. b is 24x1 but you are trying to fill all 4 columns of a
How about just
a = ones(24,4);
or if you really want to "fill" starting with zeros.
a = zeros(24,4);
b = ones(24,1);
b = repmat(b,1,4);
a = b;

Carlos Oliveira
Carlos Oliveira el 11 de Nov. de 2013
Thank you Dale. :)

Categorías

Más información sobre Loops and Conditional Statements 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