elements >0 are present in matrix only once

1 次查看(过去 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...

采纳的回答

Bruno Luong
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))))
is_all_positive_elements_nonrepeated = logical
1

更多回答(3 个)

John D'Errico
John D'Errico 2023-8-22
编辑:John D'Errico 2023-8-22
  1. Count the number of non-zero unique elements.
  2. Count the number of non-zero elements.
  3. 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
NNZ Number of nonzero matrix elements. nz = NNZ(S) is the number of nonzero elements in S. The density of a sparse matrix S is nnz(S)/prod(size(S)). See also NONZEROS, NZMAX, SIZE. Documentation for nnz doc nnz Other uses of nnz codistributed/nnz gpuArray/nnz symfun/nnz tall/nnz embedded.fi/nnz sym/nnz
How can you count the number of unique non-zero elements? Almost as easy. Start with unique.
help unique
UNIQUE Set unique. C = UNIQUE(A) for the array A returns the same values as in A but with no repetitions. C will be sorted. C = UNIQUE(A,'rows') for the matrix A returns the unique rows of A. The rows of the matrix C will be in sorted order. [C,IA,IC] = UNIQUE(A) also returns index vectors IA and IC such that C = A(IA) and A = C(IC) (or A(:) = C(IC), if A is a matrix or array). [C,IA,IC] = UNIQUE(A,'rows') also returns index vectors IA and IC such that C = A(IA,:) and A = C(IC,:). [C,IA,IC] = UNIQUE(A,OCCURRENCE) and [C,IA,IC] = UNIQUE(A,'rows',OCCURRENCE) specify which index is returned in IA in the case of repeated values (or rows) in A. The default value is OCCURRENCE = 'first', which returns the index of the first occurrence of each repeated value (or row) in A, while OCCURRENCE = 'last' returns the index of the last occurrence of each repeated value (or row) in A. [C,IA,IC] = UNIQUE(A,'stable') returns the values of C in the same order that they appear in A, while [C,IA,IC] = UNIQUE(A,'sorted') returns the values of C in sorted order. If A is a row vector, then C will be a row vector as well, otherwise C will be a column vector. IA and IC are column vectors. If there are repeated values in A, then IA returns the index of the first occurrence of each repeated value. [C,IA,IC] = UNIQUE(A,'rows','stable') returns the rows of C in the same order that they appear in A, while [C,IA,IC] = UNIQUE(A,'rows','sorted') returns the rows of C in sorted order. The behavior of UNIQUE has changed. This includes: - occurrence of indices in IA and IC switched from last to first - IA and IC will always be column index vectors If this change in behavior has adversely affected your code, you may preserve the previous behavior with: [C,IA,IC] = UNIQUE(A,'legacy') [C,IA,IC] = UNIQUE(A,'rows','legacy') [C,IA,IC] = UNIQUE(A,OCCURRENCE,'legacy') [C,IA,IC] = UNIQUE(A,'rows',OCCURRENCE,'legacy') Examples: a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2 1] [c1,ia1,ic1] = unique(a) % returns c1 = [1 2 4 5 6 7 8 9] ia1 = [21 20 19 17 14 11 7 1]' ic1 = [8 8 8 8 8 8 7 7 7 7 6 6 6 5 5 5 4 4 3 2 1]' [c2,ia2,ic2] = unique(a,'stable') % returns c2 = [9 8 7 6 5 4 2 1] ia2 = [1 7 11 14 17 19 20 21]' ic2 = [1 1 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 6 7 8]' c = unique([1 NaN NaN 2]) % NaNs compare as not equal, so this returns c = [1 2 NaN NaN] Class support for input A: - logical, char, all numeric classes - cell arrays of strings -- 'rows' option is not supported for cell arrays - objects with methods SORT (SORTROWS for the 'rows' option) and NE -- including heterogeneous arrays See also UNIQUETOL, INTERSECT, ISMEMBER, ISMEMBERTOL, UNION, SETDIFF, SETXOR, SORT, SORTROWS. Documentation for unique doc unique Other uses of unique calendarDuration/unique distributed/unique sym/unique categorical/unique duration/unique symfun/unique cell/unique fixed.Interval/unique tabular/unique dataset/unique gpuArray/unique tall/unique datetime/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))
ans = logical
1
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)))
ans = logical
1
Again, a true result, so the elements are all unique. This works because unique sorts the values as it returns them.
  1 个评论
Walter Roberson
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
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.

aldo
aldo 2023-8-22
a=magic(30)
b=unique(a)
v=numel(find(a))==numel(find(b))
do you like this code?
  9 个评论
aldo
aldo 2023-8-23
编辑:aldo 2023-8-23
i dont' understand :(...
why this code don't work?
b=unique(a)
v=numel(find(a))==numel(find(b))
using "find(a) and find(b)" it find all element >0 in a and in B
i write the my matrix can't to have value negative..
then why you say "it' don't work correctly"?
Walter Roberson
Walter Roberson 2023-8-23
Under the condition that elements in a cannot be negative:
nnz(a) == nnz(unique(a))

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by