Generating matrix with ones and zeros
1 view (last 30 days)
Show older comments
Michael Henry
on 14 Dec 2020
Commented: Star Strider
on 15 Dec 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!
0 Comments
Accepted Answer
More Answers (1)
Bruno Luong
on 14 Dec 2020
Edited: Bruno Luong
on 14 Dec 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
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!