% search_demo
% This script three ways to search data with Matlab.
% It uses the data set VP_data.mat 
% The tasks for this example are:
% 1. What is the total percentage of correct detections?
% 2. What is the percentage of correct detections for stimulus condition 3?
% 3. Are the reaction times different for correct versus incorrect responses?

% This script is part of the Matlab course for PhD Students 2010-2011:
% http://www.sinnesphysiologie.uni-oldenburg.de/50246.html
% written by Jutta Kretzberg, 14.12.2010

%% load data

load VP_data

%% Loops

[rows,cols]=size(VP_data);

% initialize counters:
lcor_dect=0;         % counter for correct detections
lcor_dect_3=0;       % counter for correct detections in condition 3
lstim_3=0;           % counter for number of condition 3 occurrences
lrt_cor=[];          % vector of reaction times for correct responses
lrt_false=[];        % vector of reaction times for false responses

% loop over all rows
for z=1:rows
    if VP_data(z,2)==1                      % correct responses
        lcor_dect=lcor_dect+1;              % increase counter of corr resp
        lrt_cor=[lrt_cor VP_data(z,3)];     % attach reaction time to vector
        if VP_data(z,1)==3;                 % specifically for stim 3
            lcor_dect_3=lcor_dect_3+1;      % counter of correct reactions to 3    
            lstim_3=lstim_3+1;               % counter for occurrences of 3
        end
    else                                    % false responses
        lrt_false=[lrt_false VP_data(z,3)]; % attach reaction time to vector
        if VP_data(z,1)==3;                 % occurences of stim 3 must also
            lstim_3=lstim_3+1;               % be counted if response was false 
        end
    end
end

% calculate percentages: 100*number of correct responses / total presentation number
lcor_perc=100*lcor_dect/rows         
lcor_3_perc=100*lcor_dect_3/lstim_3

% calculate mean reaction times:
lrt_cor_mean=mean(lrt_cor)
lrt_false_mean=mean(lrt_false)
    

%% Logical matrix operations

% percentage of correct responses:
mcor_number=sum(VP_data(:,2));       % counts correct responses by summing 1s
mcor_perc=100*mcor_number/length(VP_data)

% percentage of correct responses for stim 3:
log_ind=VP_data(:,1)==3;            % find stimulus condition 3. log_int is
                                    % a 500x1 logical vector, with 1 for 
                                    % stimulus condition 3 and 0 otherwise
mstim_3=sum(log_ind);               % number of 1s in vector is number of 
                                    % stim 3 ocurrences
mstim3_resp=VP_data(log_ind,2);     % use logical indexing to extract vector 
                                    % of responses to stimulus 3
mcor_stim3=sum(mstim3_resp);             % number of correct responses to stim 3
mcor_3_perc=100*mcor_stim3/mstim_3  % percentage

% reaction times:
mcor_ind= logical(VP_data(:,2));    % transform column 2 into logical vector
                                    % trials with correct responses are
                                    % true
mfalse_ind=~mcor_ind;               % negation of the vector with correct 
                                    % trials gives the false trials
                                    
mrt_cor=VP_data(mcor_ind,3);        % extract vector of reaction times of 
                                    % trials with corect response, using
                                    % logical indexing
                                    
mrt_false=VP_data(mfalse_ind,3);        % extract vector of reaction times of 
                                    % trials with corect response, using
                                    % logical indexing
                                    
% calculate mean reaction times:
mrt_cor_mean=mean(mrt_cor)
mrt_false_mean=mean(mrt_false)

%% find command

% percentage of correct responses:
fcor_list=find(VP_data(:,2));                   % list of indices of 
                                                % correct trials
fcor_perc=100*length(fcor_list)/length(VP_data) % percentage by using length
                                                % of the index list

% percentage of correct responses to stim 3:    
fstim3_list=find(VP_data(:,1)==3);                      % list of indices 
                                                        % with stim 3
fcor_3_list=find(VP_data(:,1)==3 & VP_data(:,2)==1);
fstim3_list=find(VP_data(:,1)==3);                      % list of indices 
                                                        % with stim 3 and
                                                        % correct response
fcor_3_perc=100*length(fcor_3_list)/length(fstim3_list)  % percentage by 
                                                        % using lengths
                                                        % of the index
                                                        % lists
                                                        
% reaction times:
ffalse_list=find(VP_data(:,2)==0);              % list of indices of 
                                                % false trials
frt_cor=VP_data(fcor_list,3);                   % apply found indices to 
                                                % extract vector of
                                                % reaction times
frt_false=VP_data(ffalse_list,3);               % apply found indices to 
                                                % extract vector of
                                                % reaction times
% calculate mean reaction times:
frt_cor_mean=mean(frt_cor)
frt_false_mean=mean(frt_false)                                                
