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.
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 Exchange에서 Logical에 대해 자세히 알아보기

질문:

2020년 12월 14일

댓글:

2020년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by