Comparing Values in Two Arrays within an If/Else
Show older comments
I am trying to generate an if/else statement involving two arrays and am wondering if there is a way to compare specific values within two arrays. The arrays I'm working with are very large, and I want the halfway point within each array to be the switch in the if/else statement.
tTreat = ones(1,10001) * 50;
tspan = 0:0.01:100;
if tspan < tTreat
A2 = 0;
else
A2 = 10;
end
Both tspan and tTreat are coming up as matrices of size 1 x 10001 as I want them to, but I'm unsure how to represent that A2 = 0 for all values of tspan where it is less than 50. Thanks in advance.
Answers (1)
Walter Roberson
on 20 Feb 2017
Edited: Walter Roberson
on 20 Feb 2017
tTreat = ones(1,10001) * 50;
tspan = 0:0.01:100;
%notice this is the opposite condition. The normal condition is to
%put in 0, so we only have to put in 10 for the other places
mask = tspan >= tTreat;
A2 = zeros(size(tspan));
A2(mask) = 10;
... just like I showed in your other question.
Another approach, with no initialization:
A2 = 10 * (tspan >= tTreat);
This depends upon the fact that you want 0 for the typical case. The code I posted first using zeros() would be easily modified to use some other basic value, but this code I show here is more difficult to adjust for other values.
7 Comments
Matt
on 20 Feb 2017
Walter Roberson
on 20 Feb 2017
I copied and pasted my code and it works perfectly. If you are using a modified version of the code then you need to post that.
Matt
on 20 Feb 2017
Image Analyst
on 20 Feb 2017
Note that if tTreat is 50 for all elements, you can simply use a scalar rather than a 10001 element array:
tTreat = 50;
The rest of Walters code will work fine.
Walter Roberson
on 20 Feb 2017
tspan = 0:0.01:100;
num_time = length(tspan);
Xind = zeros (138, num_time);
tTreat = 50;
mask = tspan >= tTreat;
A2 = zeros(size(tspan));
A2(mask) = 10;
Xind(118, :) = 10 + A2;
And then it is not clear how you are using Xind.
"The list of variables are initial conditions for use in an ODE (for which I am using ode15s):"
If you are using Xind(118) in the objective function and you want Xind(118) to be 10 for t < tTreat and you want it to be 20 for t >= tTreat, then chances are high that you have a discontinuity in your objective function at time tTreat exactly.
None of the ode*() routines can handle discontinuities in the objective function. The first derivative of the objective function must be continuous for the ode*() routines to work.
If you have a time-varying value, then you need to break your system up into two cases:
A2 = 0;
Xind(118) = 10 + A2;
first_fun = @(t, x) RemyelinationToolboxHalfTreat_eq(t, x, ..., Xind, ...);
overall_tspan = 0:0.01:100;
mask = tspan <= tTreat;
first_tspan = overall_tspan(mask);
[first_t, first_X] = ode15s(first_fun, first_tspan, X0);
A2 = 10;
Xind(118) = 10 + A2;
second_fun = @(t, x) RemyelinationToolboxHalfTreat_eq(t, x, ..., Xind, ...);
second_tspan = [tTreat, overall_tspan(~mask)]; %need to include the boundary
second_X0 = first_X(end,:); %copy the boundary condition
[second_t, second_X] = ode15s(second_fun, second_tspan, second_X0);
and you would program RemyelinationToolboxHalfTreat_eq to expect Xind as one of its parameters after the first two.
If you have an ODE system that changes with time, it is mandatory that you break up the time spans at the boundary and only run one of the cases at a time.
If you have an ODE system that changes when a boundary is reached in x (such as if you are bouncing a ball and it hits an object then you do not know ahead of time when it is going to do that), then you need to use event functions that terminate the integration when the boundary is reached; and then you restart the ode for the remaining time with the new coefficients.
You could potentially need to do this several times, such as if you were bouncing a ball off of a floor, then you would need to restart at every bounce.
It is mandatory that the coefficients used in the equations be constants in time and x for any one invocation of the ode*() functions.
Matt
on 21 Feb 2017
Walter Roberson
on 21 Feb 2017
function dx = RemyelinationToolboxHalfTreat_eq(t, y, Xind, etc)
Categories
Find more on Programming 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!