How do I write an if statement that says a variable is a error given a condition?

Please how do I write this 'if statement' in MATLAB notation? I ran a simulation N times and would like to find the error rate using an 'if statement'. I ran the simulation N number of times. How do I write that Y is an error if the statement below happens? And then calculate the errors in N number of trials?
if X=1.2||Y=0 && U1>U2
%How do I write that Y is an error if the above statement happens? And then calculate the number of errors in N number of trials?
end

10 commentaires

Thank you. My question might be a bit confusing as I didn't provide the full function. The function is listed below. So I'm trying to simulate a model where
function [errorRate,rTime]= RightsimulateDDM(U1,U2,dT,a,To)
N= 1000; %number of simulations performed
rTime= zeros(1,N);%create a row vector for the reaction time of each simulation
for trialNumber= 1:N
x1(1)= 0.5*a;
x2(1)= 0.5*a;
i=1;
while x1(i) < a && x2(i) > 0
noise1= sqrt(dT)*randn;
noise2= sqrt(dT)*randn;
x1(i+1)= x(i)+ dT*(U1 -U2)+ noise1-noise2;
x2(i+1)= x2(i)+ dT*(U2 -U1)+ noise2-noise1;
i=i+1;
end
rTime(trialNumber)= To + (i-1)*dT;
if x1(i) == a || x2(i) == 0 && U1 > U2
if x2(i) == 0
disp(['Error' num2str(i)])
%Is the above if statement right?
% What I'm trying to say is that when either x1(i)=a or x2(i)=0 & U1>U2
%Then anytime x2(i)=0 occurs, it's an error.
%Then I would like to count how many errors occurred in 1000 simulations
clear x1 x2
end
end
end
Do you want
(x1(i) == a || x2(i) == 0) && U1 > U2
or
x1(i) == a || (x2(i) == 0 && U1 > U2)
?
Actualy I would like to use the code below. But I'm not entirely sure of what it means in MATLAB language. Does it mean that if x1(i)=1 or x2(i)=0, if x2(i)=0 & U1>U2, then an error occured?
if (x1(i) == a || x2(i) == 0)
if x2(i)==0 && U1 > U2
%%Then it's an error. How do I write this?
%Then I would like to count the number of errors in 1000 trials?
%ErrRate= Error count/1000
end
end
I am not clear as to whether an error should be counted if x1(i) == a but x2(i) is not 0 ?
There is no error when x1(i)==a but x2(i) is not 0
Is there are point in testing x1(i) == a then? You only count errors if x2(i) == 0 & U1 > U2 and you count those no matter what the value of x1(i) is.
So, I'm running a model where the variables are x1 and x2. In this model, an option is selected if x1 reaches the threshold ('a') or x2 reaches '0'. Whichever happens first, stops the simulation. In this model since U1>U2, x1 is the correct choice while due to noise in the system, even when U1>U2, x2 could reach the threshold first causing the wrong option to be selected. So, I'm trying to find out the trials where U1>U2 & x=0. I need to include to include trials where x(1)==a because x2==00 might have occured first.
Is my code right based on my explanation?
Actually, I think you're right. I'm a beginner on MATLAB and still trying to understand how it works. I think the correct if statement is below. How do I count the error rate:
function [x1,x2,errorRate,rTime]= RightsimulateDDM(U1,U2,dT,a,To)
N= 1000; %number of simulations performed
rTime= zeros(1,N);%create a row vector for the reaction time of each simulation
for trialNumber= 1:N
x1(1)= 0.5*a;
x2(1)= 0.5*a;
i=1;
while x1(i) < a && x2(i)<a
noise1= sqrt(dT)*randn;
noise2= sqrt(dT)*randn;
x1(i+1)= x(i)+ dT*(U1 -U2)+ noise1-noise2;
x2(i+1)= x2(i)+ dT*(U2 -U1)+ noise2-noise1;
i=i+1;
end
rTime(trialNumber)= To + (i-1)*dT;
if U1> U2 %When this is the case, x2>a is an error
if x2(i)>= a
error=1
else error=0
end
end
if U2> U1 %When this is the case, x1>a is an error
if x1(i)>= a
error=1
else error=0
end
end
errorCount= %Not sure of how to do this
errorRate= errorCount/N
clear x1 x2
end
Did my answer below not give you an idea of how to increment the errorsCount?

Connectez-vous pour commenter.

 Réponse acceptée

%%outside forloop
errCnt = 0;
%% inside forloop
if X=1.2||Y=0 && U1>U2
%How do I write that Y is an error if the above statement happens?
disp(['Error found in iteration ' num2str(i)])
% And then calculate the number of errors in N number of trials?
errCnt = errCnt + 1;
end
%%
something like that. If you're looping through Y, dont forget to change it to Y(i)

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by