Comparing Values in Two Arrays within an If/Else

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)

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

I appreciate the response, but both options still generate the error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Perhaps the real issue is that I am unsure of how to resolve this error once the above code is put into place. Use of the debugger would only help me localize the error to a specific location, would it not? And that only seems to trace back to this section of code
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.
I used exactly the code as you specified, copied and pasted. For more context:
X0 = zeros(404,1);
k = zeros(148,1);
Xind = zeros (138,1);
tTreat = ones(1,10001) * 50;
tspan = 0:0.01:100;
mask = tspan >= tTreat;
A2 = zeros(size(tspan));
A2(mask) = 10;
And what follows is a list of variables (X0, k, and Xind). The line for which I am attempting this time based change is:
Xind(118) = 10 + A2;
The list of variables are initial conditions for use in an ODE (for which I am using ode15s):
[t,X] = ode15s(@RemyelinationToolboxHalfTreat_eq, tspan, X0);
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.
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.
Thank you very much! One final question, in your response you mention "you would program RemyelinationToolboxHalfTreat_eq to expect Xind as one of its parameters after the first two". Could you elaborate a little on what you mean by that?
function dx = RemyelinationToolboxHalfTreat_eq(t, y, Xind, etc)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 20 Feb 2017

Commented:

on 21 Feb 2017

Community Treasure Hunt

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

Start Hunting!