Changing size vector. Avoiding a loop
1 回表示 (過去 30 日間)
古いコメントを表示
Hi
I have a problem that is taking hours to be solved and I am trying to gain efficience in order to make it faster. Basically, I had three loops and I was able to replace two of them by using element by element operations, but I don't know how to get ride of the last loop because I need a scalar and a vector that change in every iteration.
Do you know if there is a way to solve this problem more efficiently? I know I am not very clear but I don't know how to explain the problem better.
My code looks something like this:
T=[1:1:10];
for j=1:length(T)
s=0.001:0.001:T(j);
x1=0:0.5:5;
x1=x1';
omega=zeros(length(x1),length(s));
omega=repmat(s,length(x1),1);
x=zeros(length(x1),length(s));
x=repmat(x1,1,length(s));
omega=exp(-x.^2./2./(T(j)-omega))./((omega.*(T(j)-omega)).^(3/2));
omega=flipud(cumsum(flipud(omega')))'*0.0005;
end
Well, I'll be very glad if someone can help me a little with this. Thank you in advance Javier
3 件のコメント
dpb
2014 年 1 月 14 日
Plus, get rid of the extra rearrangements -- instead of
x=0:N;
x=x'
write
x=0:N';
from the git-go. I didn't try to read the code closely enough to try to discern the intent but I'd think you could generate the vectors in the proper order to begin with as well to get rid of the flipud as well. That seems likely to be unnecessary with forethought.
Amit
2014 年 1 月 14 日
Moreover, initialization in the loop is not going to make any difference. You can remove zeros initialization in the loop.
Also, can you post the equation you are trying to solve? That might give some ideas on how to solve this (cause x^2/2/(T-omega) seems a bit odd (double division?).
Another suggestion - profiler . That might come in handy for slow code.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!