How to check for repeated values in a matrix?

8 views (last 30 days)
I have a 3x3 matrix and i want to check if any values repeat, and if any values do not repeat then i have to check if the values are between 1 to ( r )^2 ?
How can I do that?
a=[1 4 6;2 8 9;3 5 7];
[r,c]=size(a);

Accepted Answer

Adam Danz
Adam Danz on 31 Mar 2020
Edited: Adam Danz on 31 Mar 2020
If you're working with integers, check for repeated values using unique()
repeatsTF = numel(a) == numel(unique(a));
However, if you're dealing with floating decimals, you'll need to work in a tolerance level,
repeatsTF = numel(a) == numel(uniquetol(a));
See uniquetol() for options to set the tolerance (or use the default).
In both cases, repeatsTF returns a logical scalar (true | false) indicating whether the matrix contains repeats (false).
To check if values are between 1 to ( r )^2 ?
isBetween = all(a>1 & a<r^2, 'all');
where isBetween is true when all values of a are between (but not including) the two values. If you want to include the values, use >= <=

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!