Info
This question is closed. Reopen it to edit or answer.
How to build this matrix
1 view (last 30 days)
Show older comments
Hello everyone! I'm going to ask you some advices to solve the problem i'm coping with; I have a vector and a matrix like these:
N=[4 2 3 4 5];
M=zeros(P;size(N));
I want the rows of my matrix be filled with the value 1, in correspondence of the positions between N and P. I'll explain in a clearer way. The first row of my matrix should be filled with value 1 in the positions between N(1) and P. This means that i'm gonna have the first column containing 0 as value till the N(1)-1-st row and 1 as value, from N(1)-st row till the last row. Same for the second column of my matrix, but this time i should have value 1 from N(2)-nd row till the last row, and so on, till the end. Obviously, the number of columns of the matrix is equal to the number of elements contained in the vector N. Simply, each element of the vector N contains the number of the elements of each column of the matrix that should be equal to 1. Generalizing, the i-th element of N is equal to the number of elements of the i-th column of the matrix that should be equal to 1. I'm facing problems in structuring the cycle to do this. Hope you're going to help me. Thanks a lot in advance!
2 Comments
Guillaume
on 30 May 2018
M=zeros(P;size(N));
This is complete nonsense for many reasons. Giving us meaningless code does not make things clearer. What would have been better is an example of all inputs with the desired output.
You mention P several times but have not given any example for P. However, from the latter part of your description it would appear that P is not needed.
Answers (1)
Guillaume
on 30 May 2018
If I understood correctly,
N =[4 2 3 4 5];
M = repmat(1:max(N), numel(N), 1)' <= N %requires R2016b or later
If on an earlier version than R2016b:
M = bsxfun(@le, repmat(1:max(N), numel(N), 1)', N)
1 Comment
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!