How can I have matlab identify if a specific element is repeated a specific number of times?

Lets say a I have a 10 by 10 matrix with a random combination of numbers. So I want MATLAB to check a specific number is repeated a specific times. For example, what can I type if I want MATLAB to check if the number 1 is repeated 3 times in the 10 by 10 matrix?

回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 12 月 12 日
sum(A(:) == 1) == 3

2 件のコメント

George
George 2012 年 12 月 13 日
So if I would like to see if the number 2 is in the matrix 3 times would I type
sum(A(:)==2)==3
Yes,
sum(A(:) == NumberToTestFor) == CountRequired

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

Muruganandham Subramanian
Muruganandham Subramanian 2012 年 12 月 12 日
編集済み: Muruganandham Subramanian 2012 年 12 月 12 日
try this:
find(A==1)
If you would like to know more,
>>help find

3 件のコメント

Walter Roberson
Walter Roberson 2012 年 12 月 12 日
That does not check to see if the element was repeated the correct number of times.
Muruganandham Subramanian
Muruganandham Subramanian 2012 年 12 月 12 日
編集済み: Muruganandham Subramanian 2012 年 12 月 12 日
If you need to check for no.of times, assign a variable
c=find(A==1)
size(c)
You can apply size() to an expression
size(find(A==1))
size() is, either way, going to return a vector of dimensions. Which of those elements should George check? Will the answer vary in newer MATLAB versions which are changing the rules about whether various functions return row outputs or column outputs?

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2012 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by