Remove rows or columns that contain nan in matlab

Source: Internet
Author: User

Today in the use of MATLAB and accumulated two points, now summarized as follows

1, Objective function is undefined at the initial point. Fmincon cannot continue. Error resolution

When using fmincon, this error is most likely that you solve the input matrix there are non-numeric values, such as Nan, please carefully check whether the input parameter matrix contains Nan and so on, if any will be removed.

2, then how to remove the matrix contains Nan's row or column it.

It is easier to remove Nan from a certain location, but it is more difficult to get rid of the line or column where Nan is located, provided that you are not familiar with MATLAB, and if you know it differently.

No nonsense, just say the solution, add a matrix

A=[1,2,nan;

3,nan,4;

5,6,7]

If you are a single line representing a sample, then the first and second rows have to be removed because their eigenvalues contain Nan.

Just one line of code can be done.

A= (All (~isnan (A), 2),:);

(1) It is so simple, which uses the all function, all (a,2) means that if a row of a does not have a non-0 value, then the row return value is 1, if the row contains a non-0 value, the row return value is 0, and eventually returns a column vector. Each element represents whether a row of a is a non-0 value. 1 means that a row is all a non-0 value, and 0 means no. I

(2) isNaN will not introduce, self-Baidu

(3) and all the same usage also has any, can also use any to do, extract does not contain Nan's non-0 columns, everyone will write.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.