Deleting zeros and NaN in a matrix

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

 Réponse acceptée

Image Analyst
Image Analyst le 13 Juil 2012

4 votes

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 commentaires

Pat
Pat le 14 Juil 2012
thanks Analyst
Thanks Taylor but if i read values from excel sheet i get error
B =
[ 2] [56] [NaN] [ 5] 'a'
[NaN] [ 2] [ 0] [20] 'b'
[ 10] [ 2] [ 3] [ 6] 'c'
[ 1] [20] [ 30] [40] 'd'
Undefined function or method 'isnan' for input arguments of type 'cell'
please help
Image Analyst
Image Analyst le 14 Juil 2012
Use cell2mat to extract out the first 4 columns to a numerical array. Right now it's a cell array.
Pat
Pat le 14 Juil 2012
i tried using ru comment using cell2mat for first 4 columns,i got
r
2 56 NaN 5
NaN 2 0 20
10 2 3 6
1 20 30 40
ten i tried concanateing r and last column i get error as
Error using ==> horzcat
CAT arguments dimensions are not consistent.
please help i need output for above matrix as
10 2 3 6 'c'
1 20 30 40 'd'
please help
Image Analyst
Image Analyst le 14 Juil 2012
Modifié(e) : Image Analyst le 14 Juil 2012
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Kye Taylor
Kye Taylor le 13 Juil 2012

3 votes

Try
A(any(isnan(A),2)|any(A==0,2),:) = []

4 commentaires

Pat
Pat le 14 Juil 2012
Thanks Taylor but if i read values from excel sheet i get error
B =
[ 2] [56] [NaN] [ 5] 'a'
[NaN] [ 2] [ 0] [20] 'b'
[ 10] [ 2] [ 3] [ 6] 'c'
[ 1] [20] [ 30] [40] 'd'
Undefined function or method 'isnan' for input arguments of type 'cell'
please help
Walter Roberson
Walter Roberson le 14 Juil 2012
Only if you use the "raw" array, the third one returned by xlsread(). If you use the first array returned by xlsread() then it will be a numeric array rather than a cell array.
Pat
Pat le 14 Juil 2012
Walter i tried
[num text raw]=xlsread('zz.xls');
A=raw;
nanRows = any(isnan(A), 2)
zeroRows = any(A==0, 2)
badRows = nanRows | zeroRows
A(badRows, :) = []
But i get error
??? Undefined function or method 'isnan' for input arguments of type 'cell'.
[num text raw]=xlsread('zz.xls');
A=num;
nanRows = any(isnan(A), 2)
zeroRows = any(A==0, 2)
badRows = nanRows | zeroRows
A(badRows, :) = []

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by