Check for duplicate entries in a large matrix matlab

此生再无相见时 提交于 2019-12-24 00:45:41

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/15955208/check-for-duplicate-entries-in-a-large-matrix-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!