Counting unique values across the columns of a matrix

How can I store indices of columns in a matrix containing more than 3 unique values? for example if: X =
8 2 1 11 0
8 10 11 3 4
9 14 6 1 4
4 3 15 11 0
I want Y=[2 3]

 採用された回答

Cedric
Cedric 2015 年 7 月 24 日
編集済み: Cedric 2015 年 7 月 24 日

0 投票

UPDATED as developed in the comment below
y = find( arrayfun( @(c) numel( unique( A(:, c) )), 1:size( A, 2 )) > 3 ) ;
FORMER
Assuming that the matrix is stored in variable A, here is an ugly one-liner:
>> find( arrayfun( @(c) nnz( accumarray( 1+A(:,c), 1 )) > 3, 1:size( A, 2) ))
ans =
2 3
But your condition "more than 3 unique values" with 4 rows, means "all unique per column". Is it a small study case and then you will need fewer unique elements per column, or will this always be "all unique"?
Here is another ugly solution:
>> find(sum(accumarray([1+A(:), reshape(bsxfun(@times, 1:size(A,2), ones(size(A))), [], 1)], 1)>0, 1)>3)
ans =
2 3

3 件のコメント

Matt Talebi
Matt Talebi 2015 年 7 月 24 日
Thanks Cedric for your reply, but I get error with both solution: for the first one it says: Error using accumarray First input SUBS must contain positive integer subscripts. Error in @(c)nnz(accumarray(1+Xcal(:,c),1))>3.
actually I have a dataset (as attached) with 21 observations and 297 variables- in the form of a 21*297 matrix. I'm only interested on those variables (columns) that contain more than 3 unique values (then I will refine my dataset by removing the unwanted columns before further analysis). I appreciate your input.
Cedric
Cedric 2015 年 7 月 24 日
編集済み: Cedric 2015 年 7 月 24 日
Hi Matt, I cannot see your attachment, but here is a better option which doesn't assume that elements of your array are integers starting at 0:
y = find( arrayfun( @(c) numel( unique( A(:,c) )), 1:size( A, 2 )) > 3 ) ;
PS: I suspect that the solution based on ACCUMARRAY is faster than the one based on UNIQUE, so if time is an issue, it may be worth adapting the former solution, working on a suitable offset for replacing the 1 in 1+A...
Matt Talebi
Matt Talebi 2015 年 7 月 24 日
This one works just fine! Thank you so much for your time.

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2015 年 7 月 24 日

1 投票

[~,ii] = mode(X);
out = find(size(X,1) - ii >= 3);

2 件のコメント

Matt Talebi
Matt Talebi 2015 年 7 月 24 日
Thanks Andrei for your attention. I've modified your codes by replacing ii>=3 with ii>3 (which is what I want) but still they don't return correct answer.
Andrei Bobrov
Andrei Bobrov 2015 年 7 月 24 日
編集済み: Andrei Bobrov 2015 年 7 月 24 日
>> X = [
8 2 1 11 0
8 10 11 3 4
9 14 6 1 4
4 3 15 11 0];
>> [~,ii] = mode(X);
>> out = find(size(X,1) - ii >= 3)
out =
2 3
>> out = find(size(X,1) - ii > 3)
out = [](1x0) % solution in Octave (now I can't use MATLAB)
>>
Please read about function mode from MATLAB .

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

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2015 年 7 月 24 日

編集済み:

2015 年 7 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by