MATLAB Answers


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

Asked by George
on 12 Dec 2012

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 Answers

Answer by Walter Roberson
on 12 Dec 2012

sum(A(:) == 1) == 3


on 13 Dec 2012

So if I would like to see if the number 2 is in the matrix 3 times would I type



sum(A(:) == NumberToTestFor) == CountRequired

Answer by Muruganandham Subramanian on 12 Dec 2012
Edited by Muruganandham Subramanian on 12 Dec 2012

try this:


If you would like to know more,

>>help find


That does not check to see if the element was repeated the correct number of times.

If you need to check for no.of times, assign a variable


You can apply size() to an expression


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?

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!