Referencing a Empty matrix

조회 수: 2 (최근 30일)
Zack Bayhan
Zack Bayhan 2014년 10월 31일
댓글: Star Strider 2014년 11월 1일
Not 100% sure this question will make sense however here it goes. I am attempting to write a while loop and when the loop breaks down and gets stuck the reason is because the matrix that its suppose to retrieve information from is empty. See code below but I was wondering if it's possible to send it to the if option by referencing the empty matrix so I can by pass where the code fails? I'm specifically talking about the time=[] criteria. Thanks for the help Zack
while i<50 && i>1
time=find(conb>.6);
times(i)=time(1);
x=1
if conb(i)<.6 && time=[]
i=n+1
x=1
end
end

채택된 답변

Star Strider
Star Strider 2014년 10월 31일
I can’t run your code, but if you want to avoid situations where ‘time’ is empty, change your if statement to:
if conb(i)<.6 && ~isempty(time)
  댓글 수: 1
Star Strider
Star Strider 2014년 11월 1일
I kept working on the problems with your code, and came up with some solutions:
----------------
I sort of solved one problem by changing the ODE solver and substituting a time vector for a time range:
[time,c]=ode15s(dcdt,[0:0.5:10],[1;0]);
because I couldn’t figure out why it was quitting without updating its time vector, leading me to believe it is a stiff problem and ode45 was getting stuck. It’s still having problems integrating your ODE, but it keeps working at it and doesn’t throw any errors (as long as I’ve let it run).
I have no idea what you’re doing so I can’t comment on your ODE’s structure or constants. I leave it to you to experiment with that and troubleshoot it. It might be worthwhile to plot ‘time’ and ‘c’ and see if anything looks strange.
I also have no idea what you’re doing here:
time=find(conb>.6);
times(i)=time(1);
since the assignment to ‘time’ destroys the ‘time’ vector ode15s kindly provides you. It also returns ‘time’ in that assignment as an index (because that’s what the find function returns), not an actual time. I suggest you name ‘time’ something else, either in that statement or in the ode15s output, so you don’t destroy the ode15s ‘time’ vector.
Those are the only other problems I could identify (and do my best to fix), but there could be others. I can say with some confidence that it now runs (as long as I’ve let it run) without errors. I leave you to troubleshoot the rest.

댓글을 달려면 로그인하십시오.

추가 답변 (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