MATLAB: Filtering out rows of data based on criteria from different columns

filter rowsloopStatistics and Machine Learning Toolboxtrue false

I have data from an experiment in which each row is a different trial. The table has several columns with information about the trial, and then 1000 columns of data.
I need to filter out certain trials (rows) based on various criteria in order to calculate means from valid trials only. For example, I have a column with accuracy data (0 = error, 1 = correct), and a column with response time data (need to filter out responses < 200). I assume I need to loop through each row to create a new true/false logical column based on my criteria (e.g., valid trials are accuracy == 1 & RT > 200).
Then, I need to create means of only valid trials grouped by conditions which are defined by strings in another column (e.g., create 3 means based on a valence column with "negative", "neutral" and "positive"). I know this is probably relatively simple to do, I am new to MATLAB. All suggestions appreciated.

Best Answer

Try this:
accuracy = data(:, 15); % If accuracy is in column 15.
RT = data(:, 37); % If response time is in column 37
goodRows = find(accuracy == 1 & RT > 200);
for k = goodRows
thisRow = data(k, :);
% Do something with this row.
end
for the means, you can use grpstats() if you have the Statistics and Machine Learning Toolbox. Make a column vector with 1, 2, or 3 depending on if the element is "negative", 'neutral' or 'positive'
col = 17; % Column with the words in it.
[rows, columns] = size(data);
groups = ones(rows, 1);
for k = 1 : size(data, 1)
if contains(data(k,col), 'neutral')
groups(k) = 2;
elseif contains(data(k,col), 'positive')
groups(k) = 3;
end
end
t = table(data,..... whatever
statarray = grpstats(t, groups);