Flying pig
Flying pig

Reputation: 612

How to extract valid value from a sparse vector in Matlab?

I have lots of vectors like this one below, very sparse, lots of 'NaN'. What I intend to do is to extract the valid number out of this vector, and put them into a separate vector with no 'NaN' values.

And every vector has different positions with valid number, so I can't put them into a matrix then extract rows.

Thus please help me with this!

10459865
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
 8751943
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
 6951680
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
 5991217
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
 5327653
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
     NaN
 4740048
     NaN
     NaN
 4265221
     NaN
     NaN
 3973280

Upvotes: 0

Views: 153

Answers (2)

Climbs_lika_Spyder
Climbs_lika_Spyder

Reputation: 6754

You can use the isnan() function to find out if an entry is a number. Then something like

x = vector of values;
new_x = x(~isnan(x));

new_x is a vector with only the valid numbers.

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283803

Assuming that vector is stored in variable a,

a(isfinite(a))

will extract just the valid (finite) entries.

Upvotes: 1

Related Questions