Generating matrix with ones and zeros

8 ビュー (過去 30 日間)
Michael Henry
Michael Henry 2020 年 12 月 14 日
コメント済み: Star Strider 2020 年 12 月 15 日
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!

採用された回答

Star Strider
Star Strider 2020 年 12 月 14 日
Use the blkdiag function:
v = ones(1,3);
x = blkdiag(v,v,v,v)
.
  4 件のコメント
Michael Henry
Michael Henry 2020 年 12 月 15 日
Thanks so much!
Star Strider
Star Strider 2020 年 12 月 15 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2020 年 12 月 14 日
編集済み: Bruno Luong 2020 年 12 月 14 日
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
  1 件のコメント
Michael Henry
Michael Henry 2020 年 12 月 15 日
Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by