Comparing Values in Two Arrays within an If/Else
8 views (last 30 days)
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.
0 Comments
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
Walter Roberson
on 21 Feb 2017
function dx = RemyelinationToolboxHalfTreat_eq(t, y, Xind, etc)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!