Fastest Search in Matlab

Post Reply
User avatar
opmtrader
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by opmtrader »

Hey everybody. I have a number and I'm trying to see if I can find its identical match in an array. Is this the fastest way to do so in Matlab:



if sum(the_number==the_array(:,1))>0,



If the number is in there it will return a 1. Is there a faster way? This is way faster than a for loop but I am wondering if some other function is faster than this.



Please let me know. Thanks.
User avatar
monkeyA
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by monkeyA »

that looks the fastest... although obviously there might be a better way in general it depends on the context.



however this website is an awesome reference for vectorisation code, and may help you find another way.... its well worth a look regardless



http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/tricks.html
If there was problem, Yo I'll solve it
User avatar
opmtrader
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by opmtrader »

Thanks MonkeyA. Good stuff. I think I may try the "Intersect" function or one of its neighbors "Unique", "Union", or "ISMember".



I wouldn't have discovered these without your help. Thanks.
User avatar
monkeyA
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by monkeyA »

Smiley you are very welcome
If there was problem, Yo I'll solve it
User avatar
opmtrader
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by opmtrader »

MonkeyA, I implemented what I was doing in a new way using the Intersect function and it sped my code up immensely. I'm talking something that used to take 8 hours now takes 1 hour! At 8 hours it was completely a useless process but at 1 hour the project is now viable. Thank you again.



[ Correction: 8 hours down to a 1/2 hour! This has really made my day. Does anyone know if there is an Intersect Function in Perl? ]
User avatar
monkeyA
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by monkeyA »

thats great to hear... I have always been amazed at how much i can speed up matlab when I get down to it Smiley   Out of interest, what functions did you change?... would be interested in seeing a  "before-slow", "after-fast" list !!
If there was problem, Yo I'll solve it
User avatar
opmtrader
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by opmtrader »

Well I will describe it for you.



I was reading in data from a text file. This text file had different specialized headings at each line, not standardized placement throughout.



I was accumulating data into arrays based on different categories. When I encountered a particularly commented line ("Y") I had to check to see if a part of it was also previously accumulated into another array ("X"). So I was checking this line by line (times millions of lines = millions of searches through "Y" array) using the "if sum(Y== Dead :,1))>0," command



My solution was just to collect all the "X"'s an all the "Y"'s in a single run. Then at the end of the run I simply did Intersect(X,Y) to get those values which were in both arrays.
User avatar
Baltazar
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by Baltazar »

i just played with matlab r14

i just discovered (maybe you already know that)

the profiling tool.



it seems that it could help tune the code.



another trick is to run matlab with the tag -nojvm

in that case it does not load the java virtual machine, and this improves perf.
Short Oil, Long Vinegar: Salad spread
User avatar
opmtrader
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by opmtrader »

Baltazar, yes I know of the Profiler. Isn't that a great tool? I used that to isolate my problem.



I will have to try this -nojvm trick. Would that be something I enter at the command line? Every last bit of speed helps. Thanks for the tip!
User avatar
Baltazar
Posts: 0
Joined: Thu Jan 01, 2004 12:00 am

Fastest Search in Matlab

Post by Baltazar »

yep it is if you use matlab on windows, use the command line matlab.exe -nojvm



(works also on unix if i'm correct)



it disable the virtual machine, so the GUI gets simpler but the code faster
Short Oil, Long Vinegar: Salad spread
Post Reply