Why is NaN inserted in wrong position?

조회 수: 1 (최근 30일)
Sam
Sam 2023년 8월 17일
댓글: Star Strider 2023년 8월 17일
I have a matrix
b = [1 3 0;-2 -1 5]
b =
1 3 0
-2 -1 5
When I perform the following operation
b(b(:,3)==5) = NaN;
the NaN is placed a the postion of -2. How come?
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 8월 17일
편집: Dyuman Joshi 2023년 8월 17일
"the NaN is placed a the postion of -2. How come?"
Are you sure about that? The output from the code says otherwise -
b = [1 3 0;-2 -1 5];
b(b(1,:)==5) = NaN
b = 2×3
1 3 0 -2 -1 5
No element in the 1st row of b equals to 5, so no assignment will take place.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 8월 17일
It should not do anything, because 5 is in row 2, not row 1.
That aside, you need to index into ‘b’ correctly to get the desired result —
b = [1 3 0;-2 -1 5]
b = 2×3
1 3 0 -2 -1 5
Check = b(1,:)==5 % Test Row #1
Check = 1×3 logical array
0 0 0
Check = b(2,:)==5 % Test Row #2
Check = 1×3 logical array
0 0 1
b(2,b(2,:)==5) = NaN % Use The Correct Indexing, Specifying The Correct Rows As Well As The Correct Columns
b = 2×3
1 3 0 -2 -1 NaN
.
  댓글 수: 3
dpb
dpb 2023년 8월 17일
편집: dpb 2023년 8월 17일
As @Les Beckham showed (or @Star Strider if he had generalized the row index expression).
To amplify, you wrote the LHS index as single value for a 2D array which is linear addressing on the assignment but did the search on 2D expression b(:,3) which returns a 1D (column) vector. The five was in the second row so that logical vector is [0;1] or the result of find() would return the numerical index of '2' which is the correct index into that vector.
Hence, when you wrote b(.)=nan; for the assignment, the "." placeholder was the logical vector [0;1] which is the second element in the array with linear indexing and since MATLAB is column-major storage order, that is position b(1,2) in the 2D array. Ergo, the NaN showed up where the -2 was originally, just like you asked it to! :) Of course, that wasn't what you meant, but MATLAB doesn't know that...
The correct syntax is that you must use the same addressing expression on the LHS as in the RHS to make the two positions commensurate in the portion of the array they refer to; in this case repeating the reference explicitly to column 3.
Star Strider
Star Strider 2023년 8월 17일
@Sam
The goal is to replace any entry in and only in the third column that is equal to 5.
Change the conditon statement to specify the third column, similar to the previous example —
b = [1 3 0;-2 -1 5]
b = 2×3
1 3 0 -2 -1 5
b(b(:,3)==5, 3) = NaN
b = 2×3
1 3 0 -2 -1 NaN
This is essentially the same as the original example, however specifying the third column.
To expand on this idea —
b = [1 3 0;-2 -1 5;7 4 6;5 9 5;2 5 8]
b = 5×3
1 3 0 -2 -1 5 7 4 6 5 9 5 2 5 8
b(b(:,3)==5, 3) = NaN
b = 5×3
1 3 0 -2 -1 NaN 7 4 6 5 9 NaN 2 5 8
So it replaces only the ‘5’ values in the third column, leaving all others unchanged.
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Les Beckham
Les Beckham 2023년 8월 17일
b = [1 3 0;-2 -1 5];
b(b(:,3)==5,3) = NaN % add ,3 to select only the third column for assignment
b = 2×3
1 3 0 -2 -1 NaN

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by