Changing size vector. Avoiding a loop

2 views (last 30 days)
Javier
Javier on 14 Jan 2014
Commented: Amit on 14 Jan 2014
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 Comments
dpb
dpb on 14 Jan 2014
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
Amit on 14 Jan 2014
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.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!