Get indices of matching values (vectorized find function)
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to find a function that does a "vectorized find" that would do the below:
>> a = rand(6, 1)
a =
0.4387
0.3816
0.7655
0.7952
0.1869
0.4898
>> b = randi(6, [6, 1])
b =
3
4
5
5
2
5
>> c = a(b)
c =
0.7655
0.7952
0.1869
0.1869
0.3816
0.1869
>> b_reconstructed = somefunction(a, c) % What is somefunction?
I know I can do something like
b_reconstructed = zeros(size(a));
for i = 1:numel(c)
b_reconstructed(i) = find(a == c(i), 1, 'first');
end
But that seems very inefficient if a, b, and c are very large (>10k rows)
0 commentaires
Réponse acceptée
Dyuman Joshi
le 25 Sep 2023
a = rand(6, 1)
b = randi(6, [6, 1])
c = a(b)
B = zeros(size(a));
for i = 1:numel(c)
B(i) = find(a == c(i), 1, 'first');
end
B
%Indices of elements in 'a' for each element in 'c' that is member of 'a'
[~,idx] = ismembertol(c,a)
1 commentaire
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!