Referencing a Empty matrix
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!