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

1 visualizzazione (ultimi 30 giorni)
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?

Risposte (1)

the cyclist
the cyclist il 22 Giu 2020
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".

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Prodotti


Release

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by