Filter löschen
Filter löschen

Fastest way to create N by 1 matrix which contain lots of zero without using for loop

1 Ansicht (letzte 30 Tage)
I want to express n x 1 matrix, X
X = [ a; a; a; a; b; 0; 0; 0; b; 0; 0; 0; (...) b; 0; 0; 0; b; 0; 0; 0; c; c; ] ("b; 0; 0; 0;" block is repeated N times)
What I first design is that:
A = a * ones(4,1) ; B = zeros(4,1); B(1) = b ; C = c * ones(2,1); X = [ A ; B ; B ; (...) B ; B ; C ] ;
But the problem is that I cannot set a number of center block. How can I code it?
Thanks in advance :D

Akzeptierte Antwort

OCDER
OCDER am 12 Okt. 2018
a = 1;
b = 2;
c = 3;
N = 10;
D = [repelem(a, 4, 1);
repmat([b; 0; 0; 0], N, 1);
repelem(c, 2, 1)];

Weitere Antworten (0)

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!

Translated by