Deleting certain rows with partially duplicate values
3 次查看(过去 30 天)
显示 更早的评论
If two rows have matching values in column A and in column B, I need to delete the row with the value RED. For example:
A B C D
1 March-1st 2 RED
1 March-1st 3 GREEN
3 March-5th 8 GREEN
6 March-8th 4 GREEN
2 April-3rd 5 RED
2 April-3rd 0 GREEN
Here I would need to delete row 1 and 5 beacuse they have the value RED in column D while they share values in column A and B with another row.
0 个评论
回答(1 个)
Voss
2024-3-12
I'll assume that's a table and the text entries are strings.
T = table( ...
[1;1;3;6;2;2], ...
["March-1st";"March-1st";"March-5th";"March-8th";"April-3rd";"April-3rd"], ...
[2;3;8;4;5;0], ...
["RED";"GREEN";"GREEN";"GREEN";"RED";"GREEN"], ...
'VariableNames',["A","B","C","D"])
[G,GID] = findgroups(T.A,T.B);
is_red = strcmp(T.D,"RED");
delete_row = false(size(T,1),1);
for ii = 1:numel(GID)
idx = G == GID(ii);
if nnz(idx) == 1
continue
end
delete_row(idx & is_red) = true;
end
T(delete_row,:) = []
7 个评论
Voss
2024-3-13
You should be deleting rows from TUnique instead of T.
Can you upload the xlsx file? Without the data, I can only guess.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!