How to check for repeated values in a matrix?

5 vues (au cours des 30 derniers jours)
Jose Grimaldo
Jose Grimaldo le 31 Mar 2020
Modifié(e) : Adam Danz le 31 Mar 2020
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);

Réponse acceptée

Adam Danz
Adam Danz le 31 Mar 2020
Modifié(e) : Adam Danz le 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 >= <=

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by