フィルターのクリア

How to check for repeated values in a matrix?

17 ビュー (過去 30 日間)
Jose Grimaldo
Jose Grimaldo 2020 年 3 月 31 日
編集済み: Adam Danz 2020 年 3 月 31 日
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);

採用された回答

Adam Danz
Adam Danz 2020 年 3 月 31 日
編集済み: Adam Danz 2020 年 3 月 31 日
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 >= <=

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by