how to find indices of duplicates in array and save results on a structure

조회 수: 3 (최근 30일)
Hi, I want to know how to find indices of duplicaes in a given array and save results on a structure.
For example, array A is given as below:
A=[1;5;2;5;3;3;4;1;2;1];
so there are duplicates of values: 1,2,3,5 , and I want to find the indices of duplicates directly without using 'find'
I manually wrote down on structure as below, but how can I write the code to save the indices of duplicates on a structure?
So those are my two questions.. 1) how to find indices of duplicates in array , and 2) how to save results on a structure
Many thanks:)

채택된 답변

Stephen23
Stephen23 2022년 5월 28일
A = [1;5;2;5;3;3;4;1;2;1];
[U,~,X] = unique(A);
V = 1:numel(A);
C = accumarray(X,V(:),[],@(v){v});
S = struct('value',num2cell(U),'indices',C)
S = 5×1 struct array with fields:
value indices
Checking:
S.value
ans = 1
ans = 2
ans = 3
ans = 4
ans = 5
S.indices
ans = 3×1
1 8 10
ans = 2×1
3 9
ans = 2×1
5 6
ans = 7
ans = 2×1
2 4

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by