Finding the indices of duplicate values in one array

1,167 ビュー (過去 30 日間)
Luis Alves
Luis Alves 2017 年 4 月 21 日
コメント済み: Gabor 2024 年 4 月 21 日
Given one array A=[ 1 1 2 3 5 6 7].
I need help to known the indices where there are duplicate values.
Thanks

回答 (8 件)

Stephan Koehler
Stephan Koehler 2019 年 7 月 16 日
A = [1 2 3 2 5 3]
[v, w] = unique( A, 'stable' );
duplicate_indices = setdiff( 1:numel(A), w )
this should work too, and is elegant
  2 件のコメント
Jun W
Jun W 2019 年 11 月 11 日
How about finding how many times are those elements repeated?
Image Analyst
Image Analyst 2019 年 11 月 11 日
Use histcounts and look for bins with more than 2 counts.
A = [1 2 3 2 5 3]
[counts, edges] = histcounts(A)
A =
1 2 3 2 5 3
counts =
1 2 2 0 1
edges =
Columns 1 through 5
0.5 1.5 2.5 3.5 4.5
Column 6
5.5
You can see that the bins for 2 and 3 both have 2 counts so there are multiples of 2 and 3 in A.
Note: This will find any repeats, and they don't have to be consecutive. If you want to look for consecutive repeats, call the diff() function and look for zeros.

サインインしてコメントする。


Image Analyst
Image Analyst 2018 年 5 月 11 日
編集済み: Image Analyst 2018 年 5 月 12 日
Here's one way:
A = [-2 0 1 1 2 3 5 6 6 6 7 11 40]
% Elements 3, 4, 8, 9, and 10 are repeats.
% Assume A is integers and get edges
edges = min(A) : max(A)
[counts, values] = histcounts(A, edges)
repeatedElements = values(counts >= 2)
% Assume they're integers
% Print them out and collect indexes of repeated elements into an array.
indexes = [];
for k = 1 : length(repeatedElements)
indexes = [indexes, find(A == repeatedElements(k))];
end
indexes % Report to the command window.
You get [3,4,8,9,10] as you should.
  5 件のコメント
Arthur Souza
Arthur Souza 2018 年 5 月 12 日
I have the 2013a version. and... IT WORKED! Thank you so much Image Analyst! I think my problem is solved now! Have a nice weekend!
Tyann Hardyn
Tyann Hardyn 2022 年 1 月 21 日
You save my life (indirectly) again, Mr Image Analyst. Thank you so much. You helped someone else, then your help will be a good answer for the others, like me, lol.

サインインしてコメントする。


Adam
Adam 2017 年 4 月 21 日
編集済み: Adam 2017 年 4 月 21 日
[~, uniqueIdx] = unique( A );
duplicateLocations = ismember( A, find( A( setdiff( 1:numel(A), uniqueIdx ) ) ) );
then
find( duplicateLocations )
will give you the indices if you want them rather than a logical vector.
There are probably neater methods though.
If you want only the duplicates after the first then simply
setdiff( 1:numel(A), uniqueIdx )
should do the job.
  9 件のコメント
CompViscount
CompViscount 2022 年 9 月 20 日
編集済み: CompViscount 2022 年 9 月 20 日
Commenting here as it's led me to overall the best answer here, it just has a mistake. The "find" in the 2nd line changes the values into indices before passing to ismember, which just makes the output nonsense. I removed that. Using the same numbers as image analyst above:
A=[ 1 1 2 3 5 6 6 7]
A = 1×8
1 1 2 3 5 6 6 7
[~, uniqueIdx] = unique(A);
dupeIdx = ismember( A, A( setdiff( 1:numel(A), uniqueIdx ) ) );
dupes = A(dupeIdx)
dupes = 1×4
1 1 6 6
dupeLoc = find(dupeIdx)
dupeLoc = 1×4
1 2 6 7
Gabor
Gabor 2024 年 4 月 21 日
This works, thanks

サインインしてコメントする。


Jan
Jan 2018 年 5 月 12 日
編集済み: Jan 2021 年 7 月 2 日
function Ind = IndexOfMultiples(A)
T = true(size(A));
off = false;
A = A(:);
for iA = 1:numel(A)
if T(iA) % if not switched already
d = (A(iA) == A);
if sum(d) > 1 % More than 1 occurrence found
T(d) = off; % switch all occurrences
end
end
end
Ind = find(~T);
end
If the input has more than 45 elements, this is faster:
function T = isMultiple(A)
% T = isMultiple(A)
% INPUT: A: Numerical or CHAR array of any dimensions.
% OUTPUT: T: TRUE if element occurs multiple times anywhere in the array.
%
% Tested: Matlab 2009a, 2015b(32/64), 2016b, 2018b, Win7/10
% Author: Jan, Heidelberg, (C) 2021
% License: CC BY-SA 3.0, see: creativecommons.org/licenses/by-sa/3.0/
T = false(size(A));
[S, idx] = sort(A(:).');
m = [false, diff(S) == 0];
if any(m) % Any equal elements found:
m(strfind(m, [false, true])) = true;
T(idx) = m; % Resort to original order
end
end
  2 件のコメント
GONZALEZ DE COSSIO ECHEVERRIA Francisco Jose
Any way to speed up this code? Thanks
Muhammad Imran
Muhammad Imran 2021 年 7 月 2 日
Thanks Jan

サインインしてコメントする。


MRINAL BHAUMIK
MRINAL BHAUMIK 2021 年 6 月 28 日
A=[ 1 1 2 3 5 6 7 6]
B = A'./A
B = B-diag(diag(B))
[pos1 pos2]=find(B==1)
o/p
pos1 =
2
1
8
6

Anamika
Anamika 2023 年 7 月 17 日
In MATLAB, you can find the indices of duplicate values in an array using the `find` function along with the `unique` function. Here's how you can do it:
A = [1 1 2 3 5 6 7];
% Finding the unique elements in the array
unique_elements = unique(A);
% Initializing an empty array to store the indices of duplicate values
duplicate_indices = [];
% Iterating through each unique element
for i = 1:numel(unique_elements)
% Finding the indices of occurrences of the current unique element
indices = find(A == unique_elements(i));
% If there are more than one occurrence, add the indices to the duplicate_indices array
if numel(indices) > 1
duplicate_indices = [duplicate_indices indices];
end
end
% Displaying the indices of duplicate values
disp(duplicate_indices);
Running this code will give you the indices of the duplicate values in the array A. In this case, the output will be: 1 2
This means that the duplicate values are located at indices 1 and 2 in the array A.

Piotr
Piotr 2023 年 5 月 11 日
Hello,
here is my attempt to solve it. I faced similar problem but in my case I wanted to have the result in two column representation. Each row contains indices of repeated values.
A = [ 1 1 2 3 5 6 7 6];
nk = nchoosek(1:length(A),2);
nk(diff(A(nk),[],2)~=0,:) = [];
disp(nk)
Cheers, Piotr

Eduardo Gonzalez Rodriguez
Eduardo Gonzalez Rodriguez 2023 年 7 月 13 日
Here is my solution to find repeated values and their counts
function [dup, counts] = duplicates(A)
[dup,~,n] = unique(A, 'rows', 'stable');
counts = accumarray(n, 1, [], @sum);
dup(counts==1) = [];
counts(counts==1) = [];

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by