Using a forloop to delete invalid trials in multiple variables/matrixes
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I have made 2 matrixes (trialinfo and saccinfo). In trialinfo I have different trials, and in the last column a 0 or 1 is displayed. A 1 is a invalid trial, a 0 is a valid trial.

I have been able to remove the trials from this matrix info using:
trialinfo(trialinfo(:,12)==1,:) = [];
I now also want to remove those trials from the matrix 'saccinfo'. For which I cannot use the above code/statement.

So far, I have been trying to use Boolean to solve this, and I am able to selectively remove 1 trial (trial 12) with the following code:
invalidboolean = trialinfo(:,12)==1
invalidtrialstrialinfo = trialinfo(invalidboolean,1);
invalidtrial12saccinfo = saccinfo(:,1) == 12
saccinfo(invalidtrial12saccinfo,:) = [];
However I would like to have this in a loop, so that the invalid trials defined in the matrix trial info (with a 1) are also found in 'saccinfo' and removed. I hope the above explanation is clear. I am very new to Matlab, so any answers with a bit of explanation are very very appreciated :)
0 comentarios
Respuestas (2)
Adam
el 3 de Mayo de 2017
idx = ismember( trialinfo(invalidboolean,1), invalidtrialstrialinfo );
saccinfo( idx ) = [];
should work to delete them all without a for loop, if I understand what you are trying to do. Or at least some similar syntax based on using ismember to match all the invalid trials with your 1st column.
4 comentarios
Steven Lord
el 3 de Mayo de 2017
Each row of trialinfo represents the same trial as the corresponding row of saccinfo, so for example row 3 of trialinfo matches with row 3 of saccinfo? If so, this is easy to do with logical indexing. Let me show you with a smaller example.
% Generate two sample matrices
A = magic(5)
B = rand(5)
% Generate a logical "mask" of rows to remove
% If you look at the original matrix A, the last two rows have a number
% less than 13 in their fifth column. This means rowsToRemove will have
% three false (0) values followed by two true (1) values.
rowsToRemove = A(:, 5) < 13
% Use the logical mask to remove the corresponding rows from A and B
A(rowsToRemove, :) = []
B(rowsToRemove, :) = []
I generated the mask as a separate variable so that it would not change when I modified A using it. If I'd replaced rowsToRemove in those last two lines with the logical expression, B would have been unchanged.
You can use this same technique but in reverse: rather than identifying rows to remove, identify rows to keep.
% Generate two sample matrices
A = magic(5)
B = rand(5)
% Generate a logical "mask" of rows to keep
rowsToKeep = ~(A(:, 5) < 13)
% In this case you could have used A(:, 5) >= 13 instead
% Use the logical mask to extract the corresponding rows from A and B
A = A(rowsToKeep, :)
B = B(rowsToKeep, :)
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!