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

조회 수: 2 (최근 30일)
GEORGIOS BEKAS
GEORGIOS BEKAS 2020년 6월 22일
답변: the cyclist 2020년 6월 22일
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에서 Multidimensional Arrays에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by