Effacer les filtres
Effacer les filtres

How can I add zeros between elements of a matrix?

43 vues (au cours des 30 derniers jours)
Lucrezia Cester
Lucrezia Cester le 31 Août 2020
Réponse apportée : Stephen23 le 31 Août 2020
I have a vector [1,2,3];
and I want to obtain [1,0,2,0,3,0];
How can this be achieved?

Réponses (2)

Stephen23
Stephen23 le 31 Août 2020
>> A = [1,2,3];
Method one: indexing:
>> B = zeros(size(A).*[1,2]);
>> B(1:2:end) = A
B =
1 0 2 0 3 0
Method two: reshape:
>> B = A;
>> B(2,:) = 0;
>> B = B(:).'
B =
1 0 2 0 3 0

madhan ravi
madhan ravi le 31 Août 2020
reshape([vector; zeros(size(vector))], 1, [])

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by