fill column elements with the column number?

How can I fill column elements with the column number. First column contains only 1, second only 2, third only 3, etc.

1 comentario

Dmitry Timofeev
Dmitry Timofeev el 14 de Sept. de 2020
Is it possible to do this without cycle? I mean without 'for'?

Iniciar sesión para comentar.

 Respuesta aceptada

Hayden Garmon
Hayden Garmon el 14 de Sept. de 2020
Editada: Hayden Garmon el 14 de Sept. de 2020
m=5;% m is length of columns
n=4;% n is number of columns
for i =1:n
A(:,i)=ones(m,1)*i ;
end
A =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

4 comentarios

Dmitry Timofeev
Dmitry Timofeev el 14 de Sept. de 2020
Is it possible to do this without cycle? I mean without 'for'?
m=5;
n=4;
i =1:n; % n is number of columns
A(:,i)=ones(m,1)*i ;% m is length of columns
% Yes it is
Dmitry Timofeev
Dmitry Timofeev el 14 de Sept. de 2020
Thanks!
madhan ravi
madhan ravi el 14 de Sept. de 2020
Editada: madhan ravi el 14 de Sept. de 2020
+1
A = ones(m, 1) * ii
%or
A = repmat(ii, m, 1)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Sept. de 2020

Editada:

el 14 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by