Using a forloop to delete invalid trials in multiple variables/matrixes

16 ビュー (過去 30 日間)
Marjoleine Hakkenberg
Marjoleine Hakkenberg 2017 年 5 月 3 日
コメント済み: Adam 2017 年 5 月 3 日
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 :)

回答 (2 件)

Adam
Adam 2017 年 5 月 3 日
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 件のコメント
Marjoleine Hakkenberg
Marjoleine Hakkenberg 2017 年 5 月 3 日
Thanks again for your response, I'm getting an error: Error using == Matrix dimensions must agree.
And I can imagine, since my 'trialinfo' matrix has more rows than my 'saccinfo' matrix does, is this easy fixable?
Adam
Adam 2017 年 5 月 3 日
Assuming k is your loop control it should be a scalar, not a matrix.

サインインしてコメントする。


Steven Lord
Steven Lord 2017 年 5 月 3 日
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, :)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by