elements >0 are present in matrix only once
2 ビュー (過去 30 日間)
古いコメントを表示
a= [3 5 9 12 0 0 0 0
4 6 7 8 10 11 0 0
15 17 18 19 0 0 0 0
16 21 22 27 28 34 35 36
23 25 30 32 0 0 0 0
38 42 44 45 46 50 51 0
39 40 41 0 0 0 0 0
43 48 49 0 0 0 0 0
54 55 60 64 65 0 0 0
56 57 58 59 68 0 0 0
73 75 77 79 80 81 82 0
74 76 78 0 0 0 0 0
]
i want to know if elements >0 are present only once...
0 件のコメント
採用された回答
Bruno Luong
2023 年 8 月 22 日
編集済み: Bruno Luong
2023 年 8 月 22 日
a= [3 5 9 12 0 0 0 0
4 6 7 8 10 11 0 0
15 17 18 19 0 0 0 0
16 21 22 27 28 34 35 36
23 25 30 32 0 0 0 0
38 42 44 45 46 50 51 0
39 40 41 0 0 0 0 0
43 48 49 0 0 0 0 0
54 55 60 64 65 0 0 0
56 57 58 59 68 0 0 0
73 75 77 79 80 81 82 0
74 76 78 0 0 0 0 0
];
is_all_positive_elements_nonrepeated = all(diff(sort(a(a>0))))
その他の回答 (3 件)
John D'Errico
2023 年 8 月 22 日
編集済み: John D'Errico
2023 年 8 月 22 日
- Count the number of non-zero unique elements.
- Count the number of non-zero elements.
- Are those two numbers the same? In either case, you have your answer.
How can you count the number of non-zero elements?
help nnz
How can you count the number of unique non-zero elements? Almost as easy. Start with unique.
help unique
Count the number of unique values it generates. If zero is among that list, can you just now subtract 1?
The point is, IF all alements are present only once, then the two counts of elements will be the same. But if there are some duplicates, then they will not be the same.
And of course, this presumes your matrix is entirely non-negative. However, IF there are negative values, then just turn them into zero, then perform the above steps. Simple enough in any case.
a= [3 5 9 12 0 0 0 0
4 6 7 8 10 11 0 0
15 17 18 19 0 0 0 0
16 21 22 27 28 34 35 36
23 25 30 32 0 0 0 0
38 42 44 45 46 50 51 0
39 40 41 0 0 0 0 0
43 48 49 0 0 0 0 0
54 55 60 64 65 0 0 0
56 57 58 59 68 0 0 0
73 75 77 79 80 81 82 0
74 76 78 0 0 0 0 0];
ahat = a(a>0); % save only the elements that are positive numbers.
% we need not even use nnz in the next line, since the creation of
% ahat discards all zeros.
numel(ahat) == numel(unique(ahat))
It returns true, so the positive elements were found exactly once in that array. No duplicates at all exist.
Alternatively, you could use unique and diff, then combined with all.
all(diff(unique(ahat)))
Again, a true result, so the elements are all unique. This works because unique sorts the values as it returns them.
1 件のコメント
Walter Roberson
2023 年 8 月 22 日
You can select the positive elements into a temporary variable. Then you can test whether the number of elements in that variable is the same as the number of unique elements in the variable.
Walter Roberson
2023 年 8 月 22 日
If you need to determine which entries are present more than once, one of the ways is to use findgroups and accumarray and logical indexing.
0 件のコメント
aldo
2023 年 8 月 22 日
9 件のコメント
Walter Roberson
2023 年 8 月 23 日
Under the condition that elements in a cannot be negative:
nnz(a) == nnz(unique(a))
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!