How can I expand a array with with even zero intervals in between?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Seba.V
am 15 Aug. 2019
Bearbeitet: Bruno Luong
am 15 Aug. 2019
Suppose I have an array a=[1 2 3], how can i generate a code that would add x-amout of zeros in between each value of the original array?
For example
a=[1 2 3] ----> function or loop (code) -----> a1=[1 0 0 2 0 0 3]
0 Kommentare
Akzeptierte Antwort
Bruno Luong
am 15 Aug. 2019
Bearbeitet: Bruno Luong
am 15 Aug. 2019
nz = 2;
b = a;
b(end+nz,:) = 0;
b = b(1:end-nz)
2 Kommentare
Bruno Luong
am 15 Aug. 2019
Bearbeitet: Bruno Luong
am 15 Aug. 2019
To understand you must aware that MATLAB store array column-wise, meaning
A = [1 3 5;
2 4 6];
is stored in contiguous internally 1, 2, 3, 4, 5 6, and when you RESHAPE A by
A(1:4), you'll get a row vector [1 2 3 4].
Now with that in mind, this first statement add nnz rows of zeros in B, so after reshaping you'll get NNZ zeros in between elements of the first row.
The last statement also removes the last NNZ zeros after the last element of a.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!