How to figure out which rows I delete and record them?

2 次查看(过去 30 天)
So I have the following code
function [Vnew, rows_removed] = removerows(V)
x = isfinite(V);
y = all(x,2);
Vnew=V(y,:)
rows_removed = V==Vnew
end
what this does is it takes the rows that have an NaN in them and deletes it. So for example, if my matrix
V =[ 1 9 0 8; 7 2 NaN 13; 4 2 10 0; 1 Inf 14 7 ]
then Vnew will be
Vnew = [ 1 9 0 8; 4 2 10 0; ];
I cannot however get the second part to work which would tell me which rows I deleted. SO for this example, it would be
rows_removed = [2, 4];
Any ideas? Thanks!

回答(1 个)

KSSV
KSSV 2016-12-6
You may follow something like below:
V =[ 1 9 0 8; 7 2 NaN 13; 4 2 10 0; 1 Inf 14 7 ] ;
Vnew = [ 1 9 0 8; 4 2 10 0; ];
% get row of NaN
[r1,c1] = find(isnan(V)) ;
%get inf
[r2,c2] = find(isinf(V)) ;
% remove those rows
V([r1 r2],:) = []

标签

Community Treasure Hunt

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

Start Hunting!

Translated by