How to create a matrix from given vectors
5 views (last 30 days)
Show older comments
I have a vector A= [ 2 4 1 3 ]
How can you create a matrix which are the length of the vector values with ones. the rest zeros?
i.e I want
B= [1 1 1 1; 1 1 0 1; 0 1 0 1; 0 1 0 0]
Regards
jason
Accepted Answer
Andrei Bobrov
on 25 Oct 2012
C = zeros(max(A),numel(A));
C(A + (0:numel(A)-1)*size(C,1)) = 1;
B = flipud(cumsum(C(end:-1:1,:)));
More Answers (2)
Azzi Abdelmalek
on 25 Oct 2012
Edited: Azzi Abdelmalek
on 25 Oct 2012
A= [ 2 4 1 3 ];
n=length(A);
s=meshgrid(1:n);
out=cell2mat(arrayfun(@(x,y) y<=A(x),s,s','un',0))
5 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices 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!