Unique values per row

5 ビュー (過去 30 日間)
Zee Imports
Zee Imports 2018 年 11 月 4 日
コメント済み: Zee Imports 2018 年 11 月 6 日
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 件のコメント
Stephan
Stephan 2018 年 11 月 4 日
編集済み: Stephan 2018 年 11 月 4 日
Please explain the thoughts that lead to your expected result.
Zee Imports
Zee Imports 2018 年 11 月 4 日
Hi, As there are 3x unique values in row 1, 2x unique values in row 2 and 1x unique values in row 3.
I have basically windowed an audio signal into a matrix and am now trying to find the number of unique values in each window

サインインしてコメントする。

採用された回答

Bruno Luong
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 件のコメント
Zee Imports
Zee Imports 2018 年 11 月 6 日
Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
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))
  1 件のコメント
Zee Imports
Zee Imports 2018 年 11 月 6 日
Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by