MATLAB: How to write a code to estimate parity of numbers in a sequence

accuracyodd even

Hi all,
I am trying to write a code that would give me an accuracy value (correct = 1, incorrect = 0).
In the matrix below first column represents a sequence of numbers presented to study participants and column two – task response (a key press – 1(same), 2(different), 0(no response). A judgement is made about the parity of each number in the first column (starting from the third number in the sequence) of whether the parity of this number is the same or different as the parity of the number two numbers back. For example, – is the parity of 5 same or different as 8? – the correct response would be 2 – different. In the case below the response was 1, which is incorrect, in which case I would need an output of 0 here. Next, – is the parity of 3 same or different as 0? – correct response again would be different (2) – output – 0. I would need a code that runs through the matrix in such manner.
Any suggestions on what code would do the job? Even a half solution or a hint will be appreciated.
5 0
0 0
8 1
3 0
9 0
2 0
7 0
6 0
1 0
4 2
8 0
1 2
0 2
5 0
9 0
7 0
2 0
6 0
3 0
4 0

Best Answer

bnumbers = [5 0 8 3 9 2 7 6 1 4 8 1 0 5 9 7 2 6 3 4]';
bresponse = [0 0 1 0 0 0 0 0 0 2 0 2 2 0 0 0 0 0 0 0]';
bbinary = de2bi(bnumbers);
bparity = (mod(sum(bbinary,2),2)~=0); % even parity
bcorr_resp = [0;0;abs(bparity(3:end)-bparity(1:end-2))+1];
baccuracy = (bresponse==bcorr_resp);