Info

此问题已关闭。 请重新打开它进行编辑或回答。

Hello, I tried to delete the rows in a matrix that had NaN and I found the instruction to delete it ,but I did not understand how the instruction works.Can someone please explain me that?

1 次查看(过去 30 天)
This is the instruction I found to delete rows that had NaN in matrix A A(any(isnan(A),2),:)=[]

回答(1 个)

Kaitlyn Keil
Kaitlyn Keil 2018-8-7
Starting with the furthest in:
isnan(A) creates a matrix the same shape as A with 1s in the place of any NaN and 0 elsewhere. For readability's sake, NA=isnan(A).
any(NA,2) collapses it down into a list of rows (the 2 specifies the dimension we are looking at), as described in the any documentation page. This gives us a vector where each entry represents a row, with a 1 if there is a NaN and a 0 if there is not. Again, for readability, VNA=any(NA,2)
A(VNA,:)=[] selects every row that has a NaN (using the 1s in VNA) and sets them to be an empty array, which, for MATLAB matrices, is the same as deleting them.
  2 个评论
James Tursa
James Tursa 2018-8-7
编辑:James Tursa 2018-8-7
Also +1. Additionally, note that A(indexing stuff) = [] is a special syntax in MATLAB. The following three examples all do different things:
% Example 1
A = []; % sets A to an empty double matrix
% Example 2
A(indexing stuff) = []; % special syntax, deletes specific elements of A
% Example 3
B = []; % sets B to an empty double matrix
A(indexing stuff) = B; % attempts to set specific elements of A to empty
In Example 1, when explicit indexing is not present on the lhs, MATLAB interprets the line as a direct assignment to the whole variable. The variable A will end up being a 0x0 double.
Then note the difference between Example 2 and Example 3. They are not the same. Example 2 is using [] explicitly on the rhs and MATLAB recognizes this as a special syntax that means "delete the elements of A that match the indexing expression". But Example 3 is NOT interpreted the same way as Example 2. Example 3 means "assign a 0x0 double value to the specified elements of A." Example 3 will most likely generate an error since you can't assign a 0x0 value to the indexed elements of A (probably something other than a 0x0 subset) ... you will get a dimension mismatch error.
Also note the difference between logical indexing and numeric indexing. E.g.,
% numeric indexing
A([1 3 5],:) --> the 1st and 3rd and 5th rows of A
% logical indexing
A([true false true false true],:) --> the 1st and 3rd and 5th rows of A
% logical indexing
A(logical([1 0 1 0 1]),:) --> the 1st and 3rd and 5th rows of A
% numeric indexing
A([1 0 1 0 1],:) --> generates an error since 0 is an invalid index
Note that for logical indexing you are simply matching placement spots with the logical values. The isnan() and any() functions used above return logical results, so you end up using logical indexing in the subsequent code.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by