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!

 Accepted Answer

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

4 Comments

Michael Henry
Michael Henry on 14 Dec 2020
Edited: Michael Henry on 14 Dec 2020
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.
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
.
Thanks so much!
As always, my pleasure!

Sign in to comment.

More Answers (1)

Bruno Luong
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

Categories

Find more on Operators and Elementary Operations 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!