Removing Rows from a Cell Array if there are less than 5 other rows with the same information in that column

1 view (last 30 days)
I have a 2601x19 cell array called "BP2KData"
Column 3 contains animal ID numbers
Collumn 9 contains Date Strings
All data points that do not have 4 others from that animal on that day need to be removed. For example, if there are only 3 rows with information for the animal "322" on the date "9/22" all three rows must be deleted.
My code deletes everything, every-time.
I have attached my code.
I know that this is likely a tough nut to crack, I realize they way that I am currently trying to solve this problem is grossly inefficient.
Many thanks
  1 Comment
Star Strider
Star Strider on 18 Oct 2015
Are you only looking at columns 3 and 9?
It might be more helpful for you to upload all — or a representative part — of your cell array, then we will have some idea of its structure, and we can ask you questions about it.

Sign in to comment.

Answers (1)

Jan
Jan on 18 Oct 2015
n = size(BP2KData, 1);
keep = false(1, n);
check = true(1, n); % Avoid checking multiple times
for k = 1:n
if check(k)
matchID = strcmp(BP2KData(:, 3), BP2KData(k, 3));
matchDate = strcmp(BP2KData(:, 9), BP2KData(k, 9));
match = matchID & matchDate;
if sum(match) >= 4
keep(match) = true;
check(match) = false;
end
end
end
BP2KData = BP2KData(keep, :);

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!