Generating matrix with ones and zeros

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 日

2 投票

Use the blkdiag function:
v = ones(1,3);
x = blkdiag(v,v,v,v)
.

4 件のコメント

Michael Henry
Michael Henry 2020 年 12 月 14 日
編集済み: Michael Henry 2020 年 12 月 14 日
Thanks a lot for your help Star Strider. However, is there any way that I can make v blocks changeable at the beginning of the code. I mean setting v or any other variable to increase the number of rows without adding or removing letter v to the line itself. Please note that this is a part from larger code I am writing, in which the row size is decided and may be changed from run to run.
Please see the following the example when I increase the number of rows (I added a letter v to your code):
Many thanks for your time.
Star Strider
Star Strider 2020 年 12 月 14 日
As always, my pleasure!
I am not certain what you are asking.
If you want a way to automatically specify a number of ‘v’ vectors as inputs to blkdiag, that is proving to be difficult. The ‘v’ repititions create a comma-separated list (which is what a cell array is), however blkdiag doesn’t seem to be accepting a cell array of ‘v’ vectors as an argument and doing what I want it to do with them, which is to create a block-diagonal matrix of them, simply by specifying the number of them I want.
This is as close as I can get:
v = ones(1,2);
n = 5;
repvct = mat2cell(repmat(v,1,n), 1, ones(1,n)*numel(v));
xm = blkdiag(repvct{1,1:numel(repvct)})
producing:
xm =
1 1 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 1 1
This is reasonably efficient, however it is not the single-line anonymous funciton I was hoping to be able to code.
This would have to be a separate function file with arguments ‘v’ and ‘n’:
function vm = blkmtx(v,n)
% % % v: Row vector to repeat
% % % n: Number of rows in output matrix
repvct = mat2cell(repmat(v,1,n), 1, ones(1,n)*numel(v));
vm = blkdiag(repvct{1,1:numel(repvct)});
end
The inputs would be whatever row vector you wanted ‘v’ to be, and ‘n’ the number of repititions (rows) you want in the matrix. I tested it with:
vm = blkmtx([1 2],6)
and it produced:
vm =
1 2 0 0 0 0 0 0 0 0 0 0
0 0 1 2 0 0 0 0 0 0 0 0
0 0 0 0 1 2 0 0 0 0 0 0
0 0 0 0 0 0 1 2 0 0 0 0
0 0 0 0 0 0 0 0 1 2 0 0
0 0 0 0 0 0 0 0 0 0 1 2
.
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 日

0 投票

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

カテゴリ

ヘルプ センター および 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