필터 지우기
필터 지우기

Changing size vector. Avoiding a loop

조회 수: 2 (최근 30일)
Javier
Javier 2014년 1월 14일
댓글: Amit 2014년 1월 14일
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
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
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by