Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comments
Temi O
Temi O on 8 Feb 2019
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

Sign in to comment.

Accepted Answer

Kevin Phung
Kevin Phung on 7 Feb 2019
%%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)

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!