フィルターのクリア

Test different number(values or label) in matrix?

4 ビュー (過去 30 日間)
REN
REN 2011 年 3 月 3 日
hello, I want to detect different number in matrix?
for exmaple in a matrix:
0 0 0 0 0 0 0;
3 3 0 0 0 0 0;
3 3 3 0 0 0 0;
0 0 0 0 4 4 4;
0 0 5 5 0 0 0;
5 5 5 5 5 0 0;
How can I know the number of different values in this matrix, and index for any elements with same value?
Can I use function 'diff'?

採用された回答

Paulo Silva
Paulo Silva 2011 年 3 月 3 日
number of different values, A is your matrix
numel(unique(A))
All code:
a=[0 0 0 0 0 0 0
3 3 0 0 0 0 0
3 3 3 0 0 0 0
0 0 0 0 4 4 4
0 0 5 5 0 0 0
5 5 5 5 5 0 0];
b=unique(a)
for i=2:numel(b)
c=find(a==b(i))';
fprintf('The value %d appears %d times, indices %s\n',b(i),numel(c),num2str(c))
end
  1 件のコメント
REN
REN 2011 年 3 月 3 日
thanks...

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

その他の回答 (1 件)

Matt Fig
Matt Fig 2011 年 3 月 3 日
A = [0 0 0 0 0 0 0;
3 3 0 0 0 0 0;
3 3 3 0 0 0 0;
0 0 0 0 4 4 4;
0 0 5 5 0 0 0;
5 5 5 5 5 0 0]
U = unique(A); % Has the unique elements of A.
LU = length(U); % Has the number of unique elements of A.
[L,I] = histc(A(:).',U) % Has the counts per element and locations.
  1 件のコメント
REN
REN 2011 年 3 月 3 日
just realize can not accepted two answers at same time?
Thanks Matt Fig!

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

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by