Sorting a vector by the number of time each value occurs
matlab, sorting
Solution
Here's one way of doing it:
d = randi(10,1,30); %Some fake data
n = histc(d,1:10);
[y,ii] = sort(n,'descend');
disp(ii) % ii is now sorted according to frequency
Problem
We have the following case: ``` Q = [idxcell{:,1}]; Sort = sort(Q,'descend') Sort = Columns 1 through 13 23 23 22 22 20 19 18 18 18 18 17 17 17 Columns 14 through 26 15 15 14 14 13 13 13 12 12 12 11 10 9 Columns 27 through 39 9 9 8 8 8 8 8 7 7 7 7 7 7 Columns 40 through 52 7 6 6 6 5 4 4 3 3 3 3 2 2 Columns 53 through 64 2 2 2 2 2 2 2 1 1 1 1 1 ``` How can we sort matrix Sort according to how many times its values are repeated? Awaiting result should be: ``` repeatedSort = 2(9) 7(7) 1(5) 8(5) 3(4) 18(4) 6(3) 9(3) 12(3) 13(3) 17(3) 4(2) 14(2) 15(2) 22(2) 23(2) 5(1) 10(1) 11(1) 19(1) 20(1) or repeatedSort = 2 7 1 8 3 18 6 9 12 13 17 4 14 15 22 23 5 10 11 19 20 ``` Thank you in advance.