Deleting zeros and NaN in a matrix
4 次查看(过去 30 天)
显示 更早的评论
I have a matrix containing zerros and NaN's. Now i want to delete the rows if it consists zero or Nan
for ex if i have a matrix
A=[2 3 36 9
Nan 54 20 23
85 69 10 30
20 Nan 20 30
1 0 8 20
2 6 8 9]
i want to delete rows containing zeros and Nan's
so i will have output as
out=[2 3 36 9
85 69 10 30
2 6 8 9]
Please help,i have matrix containing 1078x8 values
0 个评论
采纳的回答
Image Analyst
2012-7-13
I'd do this:
nanRows = any(isnan(A), 2)
zeroRows = any(A==0, 2)
badRows = nanRows | zeroRows
A(badRows, :) = []
Of course you could compress all that into a single line if you want but I just used separate lines for tutorial purposes so you can see what's going on. In the command window you see:
A =
2 3 36 9
NaN 54 20 23
85 69 10 30
20 NaN 20 30
1 0 8 20
2 6 8 9
nanRows =
0
1
0
1
0
0
zeroRows =
0
0
0
0
1
0
badRows =
0
1
0
1
1
0
A =
2 3 36 9
85 69 10 30
2 6 8 9
4 个评论
Image Analyst
2012-7-14
编辑:Image Analyst
2012-7-14
Why do you want to concatenate the last column when you just went to the trouble to strip it off? You don't need it to do what you asked originally.
If A is still in the workbook, you can do this first before you run my code above:
[A text raw]=xlsread('zz.xls');
to get A out of the workbook and into a variable in MATLAB.
更多回答(1 个)
Kye Taylor
2012-7-13
Try
A(any(isnan(A),2)|any(A==0,2),:) = []
4 个评论
Walter Roberson
2012-7-14
[num text raw]=xlsread('zz.xls');
A=num;
nanRows = any(isnan(A), 2)
zeroRows = any(A==0, 2)
badRows = nanRows | zeroRows
A(badRows, :) = []
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!