Finding couple of values in other vector of values
Show older comments
Say I have a list of couple of values and another list of couple of values which belong to Xd,Yd but that I don't know. How do I efficiently get the list of index locating xv,yv in Xd,Yd ?
All I could find so far is using a loop which is very slow...
[Xd,Yd] = meshgrid(linspace(0,100,101),linspace(0,100,101));
Xd=Xd(:);
Yd=Yd(:);
xv = randi(101,[20,1]);
yv = randi(101,[20,1]);
for i=1:length(xv)
Iv(i) = find(ismember(Xd,xv(i)) & ismember(Yd,yv(i))==1);
end
EDIT : this is a dummy exemple. I need to do that with a very long vectors of values.
Accepted Answer
More Answers (1)
Takes 1 millisecond (0.000897 seconds) on my computer. Why is that not fast enough for you?
There is a problem with the code in that sometimes it finds no match. What do you want to do in that situation? Here is a suggestion:
[Xd,Yd] = meshgrid(linspace(0,100,101),linspace(0,100,101));
Xd=Xd(:);
Yd=Yd(:);
xv = randi(101,[20,1]);
yv = randi(101,[20,1]);
tic
Iv = nan(length(xv), 1);
for k = 1 : length(xv)
locations = find(ismember(Xd,xv(k)) & ismember(Yd,yv(k))==1);
if ~isempty(locations)
Iv(k) = locations;
end
end
toc
6 Comments
Ouatehaouks
on 21 Dec 2021
Ouatehaouks
on 21 Dec 2021
Image Analyst
on 21 Dec 2021
Edited: Image Analyst
on 21 Dec 2021
If you're dealing with images there are built in functions to do a lot of this kind of stuff, but if you have vectors, you are pretty much limited to things like ==, contains(), ismember(), strfind(), etc. Maybe if I knew more about the actual application instead of just your attempted code at a simplified situation I could offer more.
What, exactly, is "ages" for you? Hours or days? How big is your actual data? How many gigabytes? How many rows and columns?
Ouatehaouks
on 21 Dec 2021
Image Analyst
on 21 Dec 2021
I'm not sure I'm visualizing this correctly. Do you have a diagram?
Maybe unique([x,y,z], 'rows') could help you identify duplicates and unique rows.
Ouatehaouks
on 23 Dec 2021
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!