Reputation: 367
I have a very large matrix (901x1801) which I generated by combining values of three similar arrays (with repeated values in them) to generate unique codes using a mathematical expression. The matrix is filled with these codes.
My question is... How can I check that each values of the matrix (901x1801) is unique and not repeating even a single time?
Or... Can anyone tell me how can I generate a matrix by combining three arrays of similar elements in a way that each generated value will be unique.
an early reply will be greatly appciable. Thanks in advance.
Upvotes: 2
Views: 13831
Reputation: 5001
To find the positions of duplicates in M, use the following code:
V = M(:); % flatten
[Vs, Vi] = sort(V); % sort, Vi are indices into V
delta = Vs(2:end) - Vs(1:end-1); % delta==0 means duplicate
dup1 = Vi(find(delta == 0)); % dup1 has indices of duplicates in V
dup2 = Vi(find(delta == 0) + 1); % dup2 has the corresponding other
% rewrite to [row col]
[dup1(:,1) dup1(:,2)] = ind2sub(size(M), dup1);
[dup2(:,1) dup2(:,2)] = ind2sub(size(M), dup2);
The rows of dup1 and dup2 now contain positions in M that are duplicate.
Upvotes: 4
Reputation: 12345
With a large matrix M
, to get all the unique values, use:
uniqueValues = unique(M(:));
Then, to understand if there were any repeated values, you could use:
repeatedValuesFound = numel(uniqueValues) ~= numel(M);
That is, if the array of unique values has the same number of elements as the original array, then all elements of the original array must be unique.
Upvotes: 5