Unique and Length of every rows of a matrix

Hi,
The problem I want to solve is to "filter" the rows of a matrix n x 256. The filter criteria is to keep only the rows where the number of unique elements is between a certain range.
For example, if I have a range of 2:5, and a matrix of double similar to (i know it is not n x 256 for clarity) :
A = [ [1,1,1,4,5,3,3,5,6,1], [2,4,1,5,6,6,6,8,3,2] ... ];
I want to apply unique to every rows of A:
B = [ [1,4,5,3,6], [2,4,1,5,6,7,3] ... ];
Then I want to get the length of every rows of B:
C = [5,7,...];
Then I would like to keep only the rows of A where the corresponding C is in the range. Only the values of C inside the specified range should be kept (ie: 2:5) :
D = [5,...];
I think I could use the indexes of the C matrix that satisfies the criteria to filter A, but I do not know how...
Finally, A would look like :
A = [ [1,1,1,4,5,3,3,5,6,1], ... ];
What would be the quickest way to achieve this? Thank you for your inputs!
J-P

댓글 수: 3

Oleg Komarov
Oleg Komarov 2011년 7월 23일
Is A a double or a cell array?
Is in the range, which range?
Jean-Philippe
Jean-Philippe 2011년 7월 23일
A is a matrix of double, it represents the intensity values of multiple histograms (one by row).
The range is the filter criteria, I edited the post for clarity.
Oleg Komarov
Oleg Komarov 2011년 7월 23일
Edited below

댓글을 달려면 로그인하십시오.

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 7월 23일

0 개 추천

A = randi([1 256],100,256);
szA = size(A);
B = cell(szA(1),1);
for n = 1:szA(1)
B{n} = unique(A(n,:));
end
C = cellfun('length',B);
idx = ismember(C,100:150); % range 100:150
D = A(idx,:);

댓글 수: 2

Jean-Philippe
Jean-Philippe 2011년 7월 23일
This works like a charm and execute real quickly! Thank you :)
Oleg Komarov
Oleg Komarov 2011년 7월 23일
You could also skip the B{n} and do directly C(n) = numel(unique(A(n,:)) inside the loop, C should be preallocated before as C = zeros(szA(1),1);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by