How to map a value of a vector into column number of a matrix?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mushahid Shamim
el 31 de En. de 2018
Comentada: Jan
el 1 de Feb. de 2018
let A=[2;4;5] vector I want a matrix where all the values will be zero and the 2nd 4th and 5th element of column of 10x3 matrix will be 1.
B=[0 0 0
1 0 0
0 0 0
0 1 0
0 0 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0 ]
0 comentarios
Respuesta aceptada
Jan
el 31 de En. de 2018
A = [2, 4, 5];
B = zeros(10, 3);
idx = sub2ind(size(B), A, 1:3);
B(idx) = 1;
4 comentarios
Más respuestas (2)
Andrei Bobrov
el 1 de Feb. de 2018
n = numel(A);
B = accumarray([A(:),(1:n)'],1,[10,n]);
0 comentarios
Jos (10584)
el 1 de Feb. de 2018
A fast and easy one-liner from the old days, when accumarray did not exist :)
A = [2 ; 4 ; 5]
B = full(sparse(A, 1:numel(A), 1, 10, 3))
0 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!