Optimizing a simple code
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi People
I trying to optimize the following code and get ride of the for loop. What the code dose, is simple. It estimates the next column in a matrix based on the previous column values times a matrix.
clc
clear
close all
dt=0.01;
t=0:dt:10;
sls=zeros(2,numel(t));
M_c=[0 1;0 0];
M_c=M_c+[1 0;0 1]*1/dt;
V_s=[0;1];
for i=1:numel(t)-1
sls(:,i+1)=(M_c*sls(:,i)+V_s)*dt;
end
Any suggestion for a built-in function that would do the for loop part?
댓글 수: 3
John D'Errico
2014년 12월 20일
After responding, I see it looks like Geoff and I think alike. Ergo, my condolences are due to Geoff. :)
채택된 답변
Roger Stafford
2014년 12월 20일
For your particular problem there is a way to avoid the for-loop using the colon operator, but I doubt if it is what you had in mind as a "built-in" function. Just do this:
n = numel(t);
s2 = 0:dt:(n-1)*dt;
sls = [s2.*(-dt:dt:(n-2)*dt);s2];
I tend to agree with John and Geoff that the advantage of avoiding for-loops has been greatly exaggerated among many users of matlab. It is often the very best way to accomplish a given task, and as John states, even if an advantage is gained, it is often not worth the extra programming effort to achieve.
추가 답변 (1개)
John D'Errico
2014년 12월 20일
People spend too much programmer time worrying about optimizing code that does not need it. In the end, they often get something that looks impressive, but uses as much or more time to run. Or you get something that is fine in theory, but in terms of numerical computer code does something potentially nasty.
A loop is fine there unless you are running the same code millions of times, and as long as you preallocate your sls matrix to the proper size, and you have done the latter.
On my machine, that entire code fragment took 0.0136 seconds to complete. I've spent something like a thousand times that just answering this question. If we added in the time spent by you posting it, and others reading it...
If you have something that works, USE IT. Only if it becomes a problem should you then look for bottlenecks to optimize. Remember that computer time is cheap. Programmer time is not. And, well, my time is free, worth every cent you paid for it. :)
set('soapBoxMode','down')
댓글 수: 6
Image Analyst
2014년 12월 22일
편집: Image Analyst
2014년 12월 22일
AJ1's on my black list too. Even if he does apologize for saying "Ooos I wish people without solutions just stopped answering questions! Bro you simply don't know [profanity deleted] about programming!" to John, I won't answer. And of course there was at least one other prior comment with another name calling which has since been deleted.
Roger Stafford
2014년 12월 22일
This was the original insult:
Dear John! When asking for help it is important to pose the most simple problem possible, it helps every one!. It is just unfortunate when an smart ass comes with advices rather than a solution!!!
That one needs an apology too.
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!