The experience of improving MATLAB running speed and saving space

Source: Internet
Author: User

The experience of improving MATLAB running speed and saving space

The first recommended version of Matlab 2006a, this version of the advantages of a lot (but there is a small bug, is automatically generated by the GUI m file a lot of warning, I hope in the release of the 2006b version has improved), One of the more prominent one for programmers is the automatic grammar checking function of the editing window. This avoids using variables that are not defined or assigned to some extent, and also helps you optimize your code, "scenario 3" in Example 1 is inspired by the warning of the MATLAB editing window. By the way, although MATLAB is not like other languages, the variables are "first defined, then used" rules, but my experience is that before using a variable, it is better to "define" it, where the "definition" refers to the allocation of space for it, This will not only improve the speed of the operation (this is also mentioned in the MATLAB help, see preallocating Arrays section), but also can reduce the probability of error, especially in the loop assignment, and variable size is not fixed (see this post: vib.hit.edu.cn/forum/thread-23732-1-9.html).

Here is how to speed up the problem of MATLAB, I will use two examples to illustrate.
"Example 1" task Description: Using the Imshow function to display matrix B based on the value of a
A = RANDN (100, 100);
B = zeros (Size (A));

"Scenario 1"
[x, Y] = find (A > 0.6);
For i = 1:length (X)
B (X (i), Y (i)) = 1;
End

"Scenario 2"
B = zeros (Size (A));
X = Find (A > 0.6);
B (X) = 1;

"Scenario 3":
B = zeros (Size (A));
X = Logical (A > 0.6);
B (X) = 1;

In fact, "Scenario 1" to "Scenario 2" of the improvement in the "more on the multidimensional array of MATLAB" article has been mentioned, but did not think of themselves in the matrix input notice, but in the matrix output is forgotten, it seems that the program needs to be constantly modified, optimized, skills are also need to be constantly consolidated. As for "Scenario 3", it is the warning hint that benefits from MATLAB.

However, this is not to say that all similar places can use logical instead of find, when the number of cycles associated with X, using find will be more effective, this can refer to "Example 2" of "Scenario 2", which is not achieved by logical.

"Example 2" task Description: There is a four-dimensional matrix A (size of 61*73*61*210), wherein the first three dimensions represent a cube containing the structure of the brain, the last dimension of each point in the brain corresponds to a time series of 210. There is also a three-dimensional matrix mask (size is 61*73*61), B is a value of two, of which 1 indicates that the point is the former attraction (brain), 0 means that the point is the background point. The task is to make an EMD process of the time series belonging to the former attraction in a, so as to determine whether the former site belongs to the activation area.

Obviously, this problem should be done using loops. In my written "again on the multidimensional array of MATLAB" article has been mentioned can be multidimensional array into a one-dimensional array to deal with, here, but also to use this idea. And, as can be seen from the following, it is precisely because of the "multidimensional change one-dimensional" appearance, so that the program has been further accelerated.

First, the reshape function is used to transform the four-dimensional matrix A into a two-dimension matrix B and the three-dimensional matrix mask into a one-dimensional matrix C:
B = Reshape (A, 61*73*61, 210);
C = Mask (:);

t = 1:210;

"Scenario 1"
Itotalvoxel = 61*73*61;
For k = 1:itotalvoxel
If C (k) = = 1
temp = B (k,:);
IMF = EMD (T, temp);
...
End
End
"Scenario 2"
D = Find (C);
Itotalbrainvoxel = Length (D);
For k = 1:itotalbrainvoxel
temp = B (D (k),:);
IMF = EMD (T, temp);
...
End

"Scenario 1" is clearly based on the C language routines, while "program 2" fully avoids the weakness of MATLAB-cycle, after improved ("Multidimensional to one dimension" to provide a guarantee, multidimensional words, I am afraid to use the shape of a (X (k), Y (k), Z (k),:) form), Due to the decrease in cycle times (approximately 1/3 of the original), the operating time is roughly halved.

Thus, in MATLAB, want to speed up the operation, not only to reduce the number of layers of the cycle, but also to reduce the number of cycles.

The following is a comparison of the results of the different implementations of "Example 2" (for brain Activation Zone detection): (The core work is to EMD the corresponding time series of 73,000 pre-sites, generating 10 or so of the IMF, with an average iteration of about 10 times per IMF)

Version 1: Full use of matlab Writing-run time is about 200 minutes, that is, to do 73000*100 in MATLAB, the most headache is the need to produce the iteration of the spline envelope, the default spline function time is quite many;

Version 2: matcom converted to CPP file in Borland C + + Builder run, completely out of matlab--because the spline function takes too much time, so the conversion was replaced by a polyline envelope, rather than a spline envelope, running time of about 15 minutes, However, the result is the simulation data, not the actual data, because I still did not solve the problem of matrix.h and matlib.h cannot coexist, so the actual data can not be tested, but in general, the actual data is slower than the simulation data run;

Version 3: The core code (the spline-based EMD algorithm) is made into a DLL file in MATLAB call-the entire program, from data input to data output, only one place to use a layer for loop ("Example 2" in the "Scenario 2" loop), and in combination with the above optimization scheme, The actual data can be reached in about 5 minutes.


Summary: To learn matlab, effective use of MATLAB, we must get rid of C + + ideas. Can not be used in the loop, try not to use (for example, to find extreme points, etc. these algorithms can be implemented without using loops), gradually discard the idea of C + + point-by-bit operation, more from the matrix of the whole (or sub-block) to consider. Although the MATLAB 2006a version of the cycle has been improved a lot, but the cycle is indeed the cause of the program to reduce the speed of the main reason. MATLAB provides far more than the convenience of calling the function (for example, in C + + to write FFT, DWT and other functions may require dozens of or even hundreds of lines, and in MATLAB only one or two statements), slow running may not be used well it, let it play out the cause of the director. If you want MATLAB to serve you more efficiently, then you need to constantly revise and optimize your code (my program has been written for about one weeks, while the modification, optimization of the time spent two weeks, hehe). Finally, apply someone's sentence to make a knot: instead of complaining that MATLAB is running slowly, improve your algorithm first.

The experience of improving MATLAB running speed and saving space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.