Append vector into zero matrix

9 次查看(过去 30 天)
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.

采纳的回答

Chunru
Chunru 2021-11-17
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by