How can I find indices of each unique elements in a matrix, in one shot. (efficiently)??
3 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193337/image.png)
0 个评论
采纳的回答
Stephen23
2018-8-7
编辑:Stephen23
2018-8-7
>> [~,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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!