How to create a matrix from given vectors
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Andrei Bobrov
el 25 de Oct. de 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 comentarios
Más respuestas (2)
Azzi Abdelmalek
el 25 de Oct. de 2012
Editada: Azzi Abdelmalek
el 25 de Oct. de 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 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!