Clear Filters
Clear Filters

How can I find indices of each unique elements in a matrix, in one shot. (efficiently)??

2 views (last 30 days)
How can I find indices of each unique elements in a matrix, in one shot. (efficiently)?
For explaining i made this simple script
%%Creation of 'mat'
mat = magic(10);
for i=1:10
mat(mat>(10*i-10) & mat<=10*i)=i;
end
mat_unique = unique(mat(:)); % unique values of 'mat'
%%Finding indices of each unique elements
%%Below thing I want to do in 1 shot without 'for' loop
%%because in my actual problem 'mat' size is very big.
for i=1:length(mat_unique)
index(:,i) = find(mat == mat_unique(i))
end

Accepted Answer

Stephen23
Stephen23 on 7 Aug 2018
Edited: Stephen23 on 7 Aug 2018
>> [~,idx] = sort(mat(:));
>> reshape(idx,10,10)
ans =
3 6 7 59 56 53 51 8 4 1
9 10 16 60 57 54 55 12 5 2
17 19 25 68 66 62 64 26 13 11
18 20 34 69 70 63 65 40 14 15
21 24 43 77 79 71 73 49 23 29
22 28 58 78 80 72 74 52 27 30
31 32 67 86 88 81 82 61 36 38
35 33 76 87 89 85 83 75 37 39
44 41 90 91 92 99 96 84 46 47
45 42 94 95 93 100 97 98 50 48

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!