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

2 vues (au cours des 30 derniers jours)
GEORGIOS BEKAS
GEORGIOS BEKAS le 22 Juin 2020
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?

Réponses (1)

the cyclist
the cyclist le 22 Juin 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".

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Produits


Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by