Indexing problem. I want to insert a vector into another vector with a loop.

1 次查看(过去 30 天)
I have a matrix A, whose initial form is as follows:
A = [5 4 3]
By using the following expression:
A = [A,zeros(1, 12)];
My matrix turns into:
A = [5 4 3 0 0 0 0 0 0 0 0 0 0 0 0]
I want to insert a vector
i = [1 -1 -1 1]
, and create multiple expressions of A, via a loop.
The result should be something like this:
A = [5 4 3 1 -1 -1 1 0 0 0 0 0 0 0 0]
And then like this:
A = [5 4 3 0 0 1 -1 -1 1 0 0 0 0 0 0 ]
And later like this:
A = [5 4 3 0 0 0 0 1 -1 -1 1 0 0 0 0 ]
The final form of A, should be like this:
A = [5 4 3 0 0 0 0 0 0 0 0 1 -1 -1 1]
How could I code a loop that generates these versions of A?

回答(1 个)

the cyclist
the cyclist 2020-6-22
Here is one way.
A = [5 4 3];
B = [1 -1 -1 1 0 0 0 0 0 0 0 0 0 0];
for nb = 0:numel(B)-4
output = [A circshift(B,nb)]
end
The variable output is the different "versions of A".

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by