How to create a matrix from given vectors
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
2 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 25 Okt. 2012
C = zeros(max(A),numel(A));
C(A + (0:numel(A)-1)*size(C,1)) = 1;
B = flipud(cumsum(C(end:-1:1,:)));
5 Kommentare
Weitere Antworten (2)
Azzi Abdelmalek
am 25 Okt. 2012
Bearbeitet: Azzi Abdelmalek
am 25 Okt. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!