Group cells in a matrix per column based on an indexed starting point

2 次查看(过去 30 天)
I have two logical array matrices, A & B. I have the indexes of the 1s from A. I want to use these indexes as a starting point for grouping consecutive 1s in B, and then change everything else to 0.
The 1s in A represent the starting points of my events of interest, and B has the entire events as well as some false positives. I want to use the indexes from A to get rid of the false positives by changing them to 0 if they are not part of a series of 1s that begins with one of my indexes from A.
How would you do this?

回答(1 个)

TADA
TADA 2020-1-2
you can map the ends of each series of ones using diff:
C = [diff(B, 1, 1) == -1; B(end, :)];
then check each occurence of C to see if it has a preceding index in A and no index in C between the two.
If the previous occurence of C is prior to the previous occurence of A it is a valid series
but if there is an occurence of C between the current C index and the previous occurence of A it means this is the end of a false positive chain, then you can zero out everything from the previous C index +1 up to the current C index
  2 个评论
Teddy Fisher
Teddy Fisher 2020-1-2
that is great, I think I will try this. How do you write a script for this though? I have a large amount of data and I can't manually check each occurence so i would need to have that part you described in code
TADA
TADA 2020-1-2
I think the most straight forward method would be to loop through all indices in C (or whatever you decide to call it)
something of that sort:
prevCI = 0;
aIndices = find(A);
ai = 1;
for ci = find(C)
% search for previous ai between previous ci and current ci
while prevCI >= aIndices(ai) && aIndices(ai) <= ci && ai <= numel(aIndices)
ai = ai + 1;
end
if prevCI < aIndices(ai)
% ci is valid
else
% ci is invalid, remove from B
B((prevCI+1):ci) = false;
end
prevCI = ci;
end
this code probably doesn't work out of the box though...
first of all because i didn't test it, second, it handles B as a row/column vector or assumes your "events" continue from one column to the next, regardless of that A and C were calculated column by column..
So according to your need, adapt this script or the indices in C

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by