how to find the index of first n occurrence of each unique value of a matrix

25 views (last 30 days)
I have a 5915x1 matrix and there are 42 unique values in the matrix.I want to find out first 75 occurences of each unique value. suppose i have a column matrix A as
A={1 2 3 4 1 1 2 2 5 5 5 5 4 6 7 8 4 6 4 }
Then for 1 : [1,5,6]
2:[2,7,8]
etc.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 30 Apr 2013
Edited: Azzi Abdelmalek on 30 Apr 2013
A=randi(42,5915,1);
b=cell2mat(arrayfun(@(x) find(A==x,75),1:42,'un',0))'

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 30 Apr 2013
Edited: Andrei Bobrov on 30 Apr 2013
A = randi(8,20,1); % eg
n = 4;
A1 = unique(A)
na = numel(A1);
out = [A1(:).';nan(n,na)];
for j1 = 1:na
k = find(A == A1(j1),n);
out(2:numel(k)+1,j1) = k;
end
out = out.';

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!