Referencing a Empty matrix
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 31 Oct 2014
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 Comment
Star Strider
on 1 Nov 2014
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.
More Answers (0)
See Also
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!