Unique values per row
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi all, I'm struggling to figure a simple method to determine the number of unique values per row in a matrix. For example in the matrix [1 2 3; 4 4 5; 6 6 6]
I would expect the result to be [3, 2, 1] I've tried using unique() and histc but neither are giving me the result I am looking for. Thanks!
댓글 수: 2
채택된 답변
Bruno Luong
2018년 11월 4일
>> A=[1 2 3; 4 4 5; 6 6 6]
A =
1 2 3
4 4 5
6 6 6
>> sum(diff(sort(A,2),1,2)~=0,2)+1
ans =
3
2
1
추가 답변 (1개)
Walter Roberson
2018년 11월 4일
There is a vectorized way to compute this but it is not simple and it quickly gets expensive to calculate. Easier is
cellfun(@(c) length(unique(c)), num2cell(YourMatrix, 2))
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!