List of IDs to matrix of indexes

6 visualizaciones (últimos 30 días)
Marleen van Dijk
Marleen van Dijk el 8 de Jun. de 2020
Comentada: Marleen van Dijk el 9 de Jun. de 2020
I have a list containing numbers like lijst = [1,1,1,2,2,3,3,3,3];
Now I want to make a matrix/cell array which counts up like:
A = [1,2,3;
4,5;
6,7,8,9];
(Essentially the number in the array is equal to the row in the matrix, and the number in the matrix is equal to the index of the array)
Is there a nice way to do this?

Respuesta aceptada

madhan ravi
madhan ravi el 8 de Jun. de 2020
Editada: madhan ravi el 8 de Jun. de 2020
v = 1:numel(list);
[u,~,c] = unique(list);
Wanted = arrayfun(@(x) v(list == x), u,'un',0);
% or
Wanted = accumarray(c,v,[],@(x){x.'});
celldisp(Wanted)
  3 comentarios
Marleen van Dijk
Marleen van Dijk el 9 de Jun. de 2020
I am confused now. When I try to use the function unique(ID) my output is:
"1" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "2" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "3" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "4" "40" "41" "42" "43" "44" "45" "46" "47" "48" "49" "5" "50" "6" "7" "8" "9"
Marleen van Dijk
Marleen van Dijk el 9 de Jun. de 2020
I just found out it does not work because ID consists of strings instead of doubles. Thanks for the help though!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing 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