List of IDs to matrix of indexes

6 views (last 30 days)
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;
(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?

Accepted Answer

madhan ravi
madhan ravi on 8 Jun 2020
Edited: madhan ravi on 8 Jun 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.'});
Marleen van Dijk
Marleen van Dijk on 9 Jun 2020
I just found out it does not work because ID consists of strings instead of doubles. Thanks for the help though!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by