Borrar filtros
Borrar filtros

Append vector into zero matrix

6 visualizaciones (últimos 30 días)
H Lange
H Lange el 17 de Nov. de 2021
Comentada: H Lange el 17 de Nov. de 2021
So I have a zero matrix where I wish to insert the same vector into each column of the matrix, but for each column it need to be displaced one row, so that the vectors 'start' in the diagonal of the matrix. For instance if we have the vector v = [v_1, v_2, v_3], the matrix should look as following:
How would you implement this?
So far I have written the following code (the matrix is a NxM matrix):
for i=1:N
for j=1:M
???
end
end
The loop also needs to work for other vectors that has more than 3 elements.

Respuesta aceptada

Chunru
Chunru el 17 de Nov. de 2021
v = zeros(5, 3);
x = randn(3,1);
n = numel(x);
for i=1:size(v, 2)
v(i-1+(1:n), i) = x;
end
v
v = 5×3
1.0525 0 0 1.3620 1.0525 0 0.0952 1.3620 1.0525 0 0.0952 1.3620 0 0 0.0952
  1 comentario
H Lange
H Lange el 17 de Nov. de 2021
That works perfectly, thank you so much!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by