How to identify duplicate elements index values in this array without deleting them

2 views (last 30 days)
A=[53 54 80 1
21 43 12 0
27 71 14 0
52 55 70 23
36 69 26 0
46 66 38 0
68 78 60 0
59 3 48 0
74 7 42 0
42 17 74 0
67 30 64 0
64 35 67 0]
how to identify duplicate elements index values in this array without deleting them like 67 is repeated twice and so as 64

Accepted Answer

Image Analyst
Image Analyst on 25 Jul 2021
Try this:
A = [53 54 80 1
21 43 12 0
27 71 14 0
52 55 70 23
36 69 26 0
46 66 38 0
68 78 60 0
59 3 48 0
74 7 42 0
42 17 74 0
67 30 64 0
64 35 67 0]
minValue = min(A(:))
maxValue = max(A(:))
[counts, edges] = histcounts(A, minValue : maxValue)
for k = 1 : length(counts)
if counts(k) >= 2
fprintf('%f is in there %d times.\n', edges(k), counts(k));
end
end
You'll see:
0.000000 is in there 10 times.
42.000000 is in there 2 times.
64.000000 is in there 2 times.
67.000000 is in there 2 times.
74.000000 is in there 2 times.
  3 Comments
Image Analyst
Image Analyst on 26 Jul 2021
@sakshi chopra, you didn't mention any of that originally.
To find out the rows that have duplicate values within the row, do this:
[rows, columns] = size(A)
for row = 1 : rows
% Get the whole row.
thisRow = A(row, :);
% Delete anything that has a value of 0
thisRow(thisRow == 0) = [];
% Find the number of unique values
uniqueValues = unique(thisRow);
if length(thisRow) > length(uniqueValues)
fprintf('Row %d has at least one duplicate value.n', row);
end
end
Note that no row has duplicate values in the example you gave.
If you want to find duplicates anywhere in the array, even if the duplicate lives on a different row or column, and ignore zeros, use this:
A = [53 54 80 1
21 43 12 0
27 71 14 0
52 55 70 23
36 69 26 0
46 66 38 0
68 78 60 0
59 3 48 0
74 7 42 0
42 17 74 0
67 30 64 0
64 35 67 0]
minValue = min(A(:))
maxValue = max(A(:))
[counts, edges] = histcounts(A, minValue : maxValue)
ca = cell(1, length(counts));
for k = 1 : length(counts)
if counts(k) >= 2 && edges(k) ~= 0
fprintf('%f is in there %d times at these locations.\n', edges(k), counts(k));
% Find locations where this value lives.
[r, c] = find(A == edges(k));
ca{k} = [r, c];
for k2 = 1 : length(r)
fprintf(' at row %d, column %d.\n', r(k2), c(k2));
end
end
end
You see
42.000000 is in there 2 times at these locations.
at row 10, column 1.
at row 9, column 3.
64.000000 is in there 2 times at these locations.
at row 12, column 1.
at row 11, column 3.
67.000000 is in there 2 times at these locations.
at row 11, column 1.
at row 12, column 3.
74.000000 is in there 2 times at these locations.
at row 9, column 1.
at row 10, column 3.
and ca is a cell array that contains the rows and columns that the number appears in for each number. It's a cell array because each number could appear a different number of times (otherwise you could have used a nromal double 3-D array).

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!