Borrar filtros
Borrar filtros

create a matrix with numbers from vector

1 visualización (últimos 30 días)
Elysi Cochin
Elysi Cochin el 13 de Feb. de 2019
Comentada: madhan ravi el 13 de Feb. de 2019
i have a vector as
v = [ 1 1 1 2 2 2 3 3 4]
i wanted to create a new matrix as
M = [
1 1 1 0 0 0 0 0 0;
0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 1];
how to do it?

Respuesta aceptada

madhan ravi
madhan ravi el 13 de Feb. de 2019
v = [1 1 1 2 2 2 3 3 4];
u=unique(v);
R=arrayfun(@(x)v==u(x),1:numel(u),'un',0);
M=+vertcat(R{:})
Gives:
M =
1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 1

Más respuestas (2)

madhan ravi
madhan ravi el 13 de Feb. de 2019
Simpler:
M = +(v==unique(v).')
  2 comentarios
Stephen23
Stephen23 el 13 de Feb. de 2019
+1 very tidy. I like that.
madhan ravi
madhan ravi el 13 de Feb. de 2019
Thank you!

Iniciar sesión para comentar.


KSSV
KSSV el 13 de Feb. de 2019
N = zeros(3,3,3) ;
for i = 1:3
N(i,:,i) = 1 ;
end
M = reshape(N,3,[])
  2 comentarios
KSSV
KSSV el 13 de Feb. de 2019
Give Example....by the way what is use of v here?
KSSV
KSSV el 13 de Feb. de 2019
v = [ 1 1 1 2 2 2 3 3 4] ;
v = reshape(v,[],3)' ;
N = zeros(3,3,3) ;
for i = 1:3
N(i,:,i) = v(i,:) ;
end
M = reshape(N,3,[])

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB 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!

Translated by