How to check for repeated values in a matrix?

8 visualizaciones (últimos 30 días)
Jose Grimaldo
Jose Grimaldo el 31 de Mzo. de 2020
Editada: Adam Danz el 31 de Mzo. de 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);

Respuesta aceptada

Adam Danz
Adam Danz el 31 de Mzo. de 2020
Editada: Adam Danz el 31 de Mzo. de 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 >= <=

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by