how to find indices of duplicates in array and save results on a structure
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1013840/image.png)
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:)
0 Commenti
Risposta accettata
Stephen23
il 28 Mag 2022
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)
Checking:
S.value
S.indices
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!