In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Matlab efficient programming skills sharing," interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "Matlab efficient programming skills sharing"!
Students who have used Matlab should know that Matlab is notoriously slow, but there are ways to optimize it, and here are a few optimization techniques commonly used in Matlab programming.
Before talking about optimization methods, the first thing to say is that Matlab uses tic toc to calculate the running time, which is common sense. Of course, if you want to calculate the specific time, you can use the profile tool.
vectorized operation
This should be a point that students who have used Matlab know clearly. The speed of operating vectors and matrices in Matlab is much faster than that of using the for loop, because its bottom layer calls the high-performance linear algebra library BLAS library and LAPACK library. I don't know.
memory preallocation
In Matlab we can define an empty matrix.
mtx = [];
Then we add some data to it later, and the size of this matrix can change according to how much data we fill in. Like this program below.
tic
n = 1000;
mtrx = [];
init = 1.0;
for i = 1:n
for j=1:n
mtrx(i,j) = init + 1.0;
end
end
toc
How long does this program run? 0.2 seconds on my computer.
So what's wrong with this program? That is, we do not allocate a memory space for this matrix, and in the loop, the size of the matrix is changing, which causes extra time to be wasted every time to find the memory space that meets the requirements, and the matrix after changing the size is moved to this new content space as a whole, and the original memory space is released. This will not only affect the running efficiency of the code, but also easily form memory fragments, making it more and more difficult for the program to find the memory that meets the conditions.
Therefore, it is a good habit to pre-allocate memory to the matrix before looping. If you don't have this habit, you can also check if there is a similar problem through Matlab's own code checker.
So, we should modify the program as follows:
tic
n = 1000;
mtrx = zeros(n,n);
init = 1.0;
for i = 1:n
for j=1:n
mtrx(i,j) = init + 1.0;
end
end
toc
This program only took 0.007 seconds to run, which shows how big the gap is.
column storage
Matlab default is stored by column, that is, column vectors in memory is arranged continuously, the continuous data processing is certainly fast, so we generally use column vectors when defining vectors. Compare the time it takes to operate on rows versus columns in the matrix below.
n = 10000;
mtrx = rand(n,n);
mcol = zeros(n,1);
mrow = zeros(1,n);
tic
for i=1:n
mcol(i) = sum(mtrx(:,i));
end
toc
We summed up every column in the matrix, taking 0.17 seconds.
tic
for i=1:n
mrow(i) = sum(mtrx(i,:));
end
toc
Summing up each row of the matrix took 0.8 seconds.
It can be seen that column operations are much faster than row operations.
data type
In Matlab, the default data type is double type, for the user, do not care too much about the data type is of course worry-free, but this also brings a problem is that double type takes up more memory, and may slow down the running speed of the program. So, under appropriate circumstances, we can choose the data type as logical type, character type, integer type, etc. One caveat is that a variable takes extra time to change its data type, so it's better to create a new variable.
So much for efficient programming, and more later. Here is a Matlab debug breakpoint setup problem. In a for loop, say for i=1:n, we want to enter the breakpoint at i=100. What should we use at this time? That's what we used to write.
for i=1:n
if(i==100)
pass
end
end
Set the breakpoint at the pass, but you don't have to. Matlab provides a conditional breakpoint setting method. Right-click Set Condition Breakpoint in the loop, as shown below.
Figure 1. Condition Breakpoint Setting 1
Fill in the conditions below, such as i==100.
Figure 2. Condition Breakpoint Setting 2
This way, when the program runs to i==100, it will enter the breakpoint, and there is no need to write additional statements yourself.
At this point, I believe that everyone has a deeper understanding of "Matlab efficient programming skills sharing," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.