Generating matrix with ones and zeros

12 Ansichten (letzte 30 Tage)
Michael Henry
Michael Henry am 14 Dez. 2020
Kommentiert: Star Strider am 15 Dez. 2020
Hello guys
Can you please help generating a matrix with size of (RXC) = (4X12)
Where, here the 3 ones are in first row then all zeros. The second row starts with 3 zeros then three ones then zeros to the end. This repeates for all rows. But the most improtant thing that I need to generate such pattern for any number of rows and columns. For example, for (4X8), it should look like
two ones then zeros. The seocnd row starts with two zeros then two ones then zeros to the end.
Many thanks for your help!

Akzeptierte Antwort

Star Strider
Star Strider am 14 Dez. 2020
Use the blkdiag function:
v = ones(1,3);
x = blkdiag(v,v,v,v)
.
  4 Kommentare
Michael Henry
Michael Henry am 15 Dez. 2020
Thanks so much!
Star Strider
Star Strider am 15 Dez. 2020
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Bruno Luong
Bruno Luong am 14 Dez. 2020
Bearbeitet: Bruno Luong am 14 Dez. 2020
Look at KRON
>> kron(eye(4),ones(1,3))
ans =
1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1
>> kron(eye(4),ones(1,2))
ans =
1 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1
>> kron(eye(4),[1 2])
ans =
1 2 0 0 0 0 0 0
0 0 1 2 0 0 0 0
0 0 0 0 1 2 0 0
0 0 0 0 0 0 1 2

Kategorien

Mehr zu Operators and Elementary Operations 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