How to check for repeated values in a matrix?

5 Ansichten (letzte 30 Tage)
Jose Grimaldo
Jose Grimaldo am 31 Mär. 2020
Bearbeitet: Adam Danz am 31 Mär. 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);

Akzeptierte Antwort

Adam Danz
Adam Danz am 31 Mär. 2020
Bearbeitet: Adam Danz am 31 Mär. 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 >= <=

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by