Effacer les filtres
Effacer les filtres

How to use find for a vector without a loop?

2 vues (au cours des 30 derniers jours)
ARGY B
ARGY B le 21 Août 2019
Commenté : ARGY B le 21 Août 2019
I have the following script but doesn't work (properly)
V_dates = [datenum(2030,1,1) datenum(2050,1,1) datenum(2075,1,1) datenum(2100,1,1)]
index = find(vector_with_dates == V_dates(:))
where vector_with_dates (size: 1x5000) is a vector having dates in serial date number form.
Now I would like to find the indexes of the vector_with_dates where the 4 numbers stored in V_dates are equal.
The only way I can think and it works properly is within a loop like this:
for i=1:length(V_dates)
index(i) = find(vector_with_dates == V_dates(i))
end
Is there a way to avoid the loop?
Thank you

Réponse acceptée

AbioEngineer
AbioEngineer le 21 Août 2019
Hi!
If you want to find all the locations in which vector_with_dates contains ANY of the dates in V_dates, use ismember
[tf,loc]=ismember(vector_with_dates,V_dates)
find(tf)
  1 commentaire
ARGY B
ARGY B le 21 Août 2019
Nice.. it works! thanx

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical 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!

Translated by