an Alternative funtion which is faster than "ismember"
Afficher commentaires plus anciens
Hello everybody,
I was using ismembertol with XY(Nx2) and xy(Mx2). However code never ends due to the enormous amount of data(N=400million M=80mil.).
Is there any way that I can speed this function. (The matrices are not unique)
[LIA,~]= ismembertol(XY,xy,0.00001,'ByRows',true,'OutputAllIndices',true);
Thank you for your support
20 commentaires
Walter Roberson
le 24 Nov 2022
is xy sorted in ascending order? If so then you can call an internal compiled routine, bypassing the sorting step.
Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
Modifié(e) : Bruno Luong
le 24 Nov 2022
If XY is gridded coordinates, then you can use discretize or simple division if they are uniform to determine which grid the river point belong to.
Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
Modifié(e) : Bruno Luong
le 24 Nov 2022
Yes apply discretize in 1D, whic edge points beeing the mid points of the linear grid 1D coordinates.
You do twice 1D, in x and y. Then they give you which xgrid coordinates is closest in x direction and which ygrid coordinates is the closest in y. Then combine both it provides which 2D grid (mesh)points is the closest.
Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
Modifié(e) : Bruno Luong
le 24 Nov 2022
No use the mid points of your linear xgrid NOT meshgrid X and Y. Invoke discretize in 1D.
Ahmet Hakan UYANIK
le 24 Nov 2022
Modifié(e) : Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
I repeat what I write: use the mid points of your linear xgrid
BTW Using unique(X(:) is waste of time and memory where X and Y are created with meshgrid. Use directly the linear grid argument you passed through meshgrid.
Ahmet Hakan UYANIK
le 24 Nov 2022
Modifié(e) : Ahmet Hakan UYANIK
le 24 Nov 2022
mid point is the mean of two (adjadcent) points.
Example in x; fdo the same in y then combine the results
xgrid = cumsum(randi(5,1,10))
x = min(xgrid)+rand(1,10)*(max(xgrid)-min(xgrid))
midpoints = (xgrid(1:end-1)+xgrid(2:end))/2;
edges = [-Inf midpoints Inf];
iclosest = discretize(x, edges)
xgridclosest = xgrid(iclosest);
d = abs(xgridclosest-x)
Ahmet Hakan UYANIK
le 24 Nov 2022
Modifié(e) : Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
Modifié(e) : Bruno Luong
le 24 Nov 2022
You just need to create a LIA logical array of size of you meshgrid and initialize to FALSE, then assign the closest index found by TRUE.
It Just a question of reindexing array.
LIA = false(length(ygrid),length(xgrid));
LIA(sub2ind(size(LIA, iclosest_y, iclosest_x)) = true; % iclosest_y and iclosest_x is the closest 1D indexes as I show you earlier
Steven Lord
le 24 Nov 2022
Based on the description I think the fourth and fifth output from the histcounts2 function may be of interest to you.
Ahmet Hakan UYANIK
le 24 Nov 2022
Modifié(e) : Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
Modifié(e) : Bruno Luong
le 24 Nov 2022
I don't understand "subscript it".
"It" is what?
Subscipt to do what?
Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
Modifié(e) : Bruno Luong
le 24 Nov 2022
There is a typo, missing a parenthesis.
LIA(sub2ind(size(LIA), iclosest_y, iclosest_x)) = true;
You should verify if these three things are satisfied
- size(LIA) is [6000 6000]
- iclosest_x vector of ~ 50000 values in 1:6000 (6000 is size(LIA,2))
- iclosest_y vector of ~ 50000 values in 1:6000 (6000 is size(LIA,1))
or read the doc and try to understand what each function suppose to do.
Ahmet Hakan UYANIK
le 24 Nov 2022
Bruno Luong
le 24 Nov 2022
I post a short code as answer so you can accept it. Thanks
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Descriptive Statistics and Insights dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!