The following is a question about Matlab designed to improve the computational effiency of my matlab scripts.I have several matlab scripts that will sort through an array(s) of data and either store or count that data and store the information in a new array.For example:for i=1:arraysize; if latitude(i) <=16 & latitude(i) >= 6 if longitude(i) <= 119 & longitude(i) >= 109 for j=1:50; if latitude(i) >= 6+(j-1)*0.2 & latitude(i) < 6+(j)*0.2 for k=1:50; if longitude(i) >= 109+(k-1)*0.2 & longitude(i) < 109+(k)*0.2 radarZsum(j,k) = radarZsum(j,k) + nearSurfZ(i); %<== 50x50 array + radar reflectivity input radarCounts(j,k) = radarCounts(j,k) + 1; %<== 50x50 array end <snippit removed>endThe above section of code is designed to find data points within a specified area. If the data is in that area it sorts it into the appropiate place in an array assuming a 0.2 degree resolution grib within the desired area. The first two logical parts of the loop are to weed out data that isn't in the study area to only deal with the data needed. The subsequent lines grid the data and then sum the reflectivity values (radar data input) and the # of data points that will go into that grid box.The way it's currently written it it tests to see if the data point will fit with all the grid boxes even if it's found the correct one. In the interest of saving CPU cycles, I'd like to end the loop and move to the next data point once all of the logical conditions have been met. I know I could write a mess of elseif based loops to do this instead of the for based loops, but in some of the scripts I have the arrays I want to write to are huge. Anyone know if matlab has something like a goto command or a command that tells it to end a section of a loop?How can I make this code more efficient in the matlab environment?I'd like some real quality input on this, so if you need more information to answer my question or if you just need further clarification, just let me know. I'll also welcome any URLs to relevent educational material.
1/28/2007 7:06:31 PM
Like the "break" statement?
1/28/2007 7:47:12 PM
use the code /code tagsI don't know matlab so I can't figure out what belongs to what blocks or I'd try to help.
1/28/2007 7:58:45 PM
^ too late to edit^^ The break command is exactly the kind of thing I was looking for. Now that I've done some reading, I can see some uses for the continue command as well. Hopefully, the break command will cut off about 30% of my run time.
1/28/2007 9:13:46 PM
another tidbit that doesn't apply to you in this situation, but good nonetheless.When you are going to be populating a large matrix/vector with numbers from a for loop or anywhere else for that matter, before you start, define a zero(j,k) array to pre-allocate the memory for the array. If you don't, then each time you advance to the next index, it will have to copy the entire previous array and then add in your new number. If you do this thousands of times, your script will slow to a crawl.
1/28/2007 9:58:28 PM
^ I do, I just didn't include that part of the code.
1/28/2007 9:59:56 PM
Complie it. then it will run much much faster.
1/28/2007 11:33:50 PM
^ That would defeat the purpose of using matlab.... or even perl.
1/29/2007 12:10:36 AM
What's wrong with creating a mex file for what your doing? It'll work seemlessly with the rest of your script. right? If not, explain why.
1/29/2007 12:18:05 AM
^ What's a mex file? I'm not familar.
1/29/2007 11:20:32 AM
Basically mex is a compiled function/m-file, think dll. You can take the section of code that contains the "for" loops (which are very slow in matlab), and create a separate function that does that. Then you compile that function to make a mex file. You can call the function inside your matlab script with inputs and outputs, and of course it will run faster because it is complied code.http://www.mathworks.com/support/tech-notes/1600/1605.html#intro[Edited on January 29, 2007 at 12:54 PM. Reason : can't type]
1/29/2007 12:50:45 PM
^ Those are interesting. I have some reading to do about this. I have some C++ libraries that I use to decode binary satellite data files I might throw into matlab this way to see how they run.
1/29/2007 6:45:46 PM