elseif that goes through previous statements but with different parameters

1 view (last 30 days)
I have a model that is extremely repetitive that I want to make more effecient, easier to change, and reduce likelihood of mistakes. It's a bunch of if else statements that are set up similarly but have varying parameters that represent different probabilities. Here's a snippet to try and paint the picture:
elseif e(t) == 1 %patch 1 is high and patch 2 is low
%obs going to patch 1
if rand() < p5(siminfo1state(t,j),siminfo2state(t,j))
if rand() < nn11 %nn11: prob of obs finding food in patch 1
simphystate(t+1,j) = simphystate(t,j)-1+3; %obs didn't watch and found food
siminfo1state(t+1,j) = siminfo1state(t,j)+1; %obs found food in patch 1
siminfo2state(t+1,j) = siminfo2state(t,j);
patchused(t,j) = 1;
patchfood(t,j) = 11;
info1gained(t+1,j) = info1gained(t,j)+1;
else %prob of obs not finding food in patch 1
simphystate(t+1,j) = simphystate(t,j)-1; %obs didn't watch and didn't find food
siminfo1state(t+1,j) = siminfo1state(t,j)-1; %obs didn't find food in patch 1
siminfo2state(t+1,j) = siminfo2state(t,j);
patchused(t,j) = 1;
patchfood(t,j) = 12;
info1gained(t+1,j) = info1gained(t,j)+1;
end
%obs going to patch 2
else
if rand() < nn21 %nn21: prob of obs finding food in patch 2
simphystate(t+1,j) = simphystate(t,j)-1+3; %obs didn't watch and found food
...
there are 4 different environments represented by e(t) and they all run through the same code except nn11 and nn21 change to different parameters for each of the 4 e(t). Is there a way I can do some sort of
if e(t) == 1
%go through all the code that has
%specified parameters
elseif e(t) == 2
% go through as if e(t) == 1
% but nn11 == nn12, nn21 == nn22 etc.
end

Answers (2)

Torsten
Torsten on 22 Feb 2025
Edited: Torsten on 22 Feb 2025
if e(t)==1
nn1 = nn11; nn2 = nn21;
elseif e(t)==2
nn1 = nn12; nn2 = nn22;
elseif e(t)==3
nn1 = nn13; nn2 = nn23;
elseif e(t)==4
nn1 = nn14; nn2 = nn24;
end
if rand() < p5(siminfo1state(t,j),siminfo2state(t,j))
compare = nn1;
else
compare = nn2;
end
if rand() < compare
simphystate(t+1,j) = simphystate(t,j)-1+3; %obs didn't watch and found food
siminfo1state(t+1,j) = siminfo1state(t,j)+1; %obs found food in patch 1
siminfo2state(t+1,j) = siminfo2state(t,j);
patchused(t,j) = 1;
patchfood(t,j) = 11;
info1gained(t+1,j) = info1gained(t,j)+1;
else
simphystate(t+1,j) = simphystate(t,j)-1; %obs didn't watch and didn't find food
siminfo1state(t+1,j) = siminfo1state(t,j)-1; %obs didn't find food in patch 1
siminfo2state(t+1,j) = siminfo2state(t,j);
patchused(t,j) = 1;
patchfood(t,j) = 12;
info1gained(t+1,j) = info1gained(t,j)+1;
end

Walter Roberson
Walter Roberson on 22 Feb 2025
Maybe
[~, bin] = ismember(e(t), [1 2]);
if bin > 0
if rand() < p5(siminfo1state(t,j),siminfo2state(t,j))
if rand() < nn1(bin) %nn11: prob of obs finding food in patch 1
simphystate(t+1,j) = simphystate(t,j)-1+3; %obs didn't watch and found food
siminfo1state(t+1,j) = siminfo1state(t,j)+1; %obs found food in patch 1
siminfo2state(t+1,j) = siminfo2state(t,j);
patchused(t,j) = 1;
patchfood(t,j) = 11;
info1gained(t+1,j) = info1gained(t,j)+1;
else %prob of obs not finding food in patch 1
simphystate(t+1,j) = simphystate(t,j)-1; %obs didn't watch and didn't find food
siminfo1state(t+1,j) = siminfo1state(t,j)-1; %obs didn't find food in patch 1
siminfo2state(t+1,j) = siminfo2state(t,j);
patchused(t,j) = 1;
patchfood(t,j) = 12;
info1gained(t+1,j) = info1gained(t,j)+1;
end
%obs going to patch 2
else
if rand() < nn2(bin) %nn21: prob of obs finding food in patch 2
simphystate(t+1,j) = simphystate(t,j)-1+3; %obs didn't watch and found food
end
end
end
That is, bundle the parameters nn1* and nn2* into vectors, and index the vectors according to the position matched?

Community Treasure Hunt

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

Start Hunting!