可能我没有得到一个基本的东西,但我最近发现了这种行为,现在我不明白发生了什么:
A = [3 NaN .12 NaN NaN 9]
A =
3 NaN 0.12 NaN NaN 9
>> nansA = isnan(A)
nansA =
0 1 0 1 1 0
>> nnansA = ~isnan(A)
nnansA =
1 0 1 0 0 1
>> nnansA1 = ~isnan(A(1:end))
nnansA1 =
1 0 1 0 0 1
>> nnansA2 = ~isnan(A(2:end))
nnansA2 =
0 1 0 0 1
>> AnnansA1 = A(~isnan(A(1:end)))
AnnansA1 =
3 0.12 9
>> **AnnansA2 = A(~isnan(A(2:end)))
AnnansA2 =
NaN NaN
这里发生了什么?
在Matlab中也会发生这种情况吗?
我想要... AnnansA2 = 0.12 9
这样的东西
这里发生的情况是,您正在使用不同大小的逻辑数组对A
进行索引,并期望索引不会从一开始就开始。
让我们从内部解构:
>> A = [3 NaN .12 NaN NaN 9]
A =
3.0000 NaN 0.1200 NaN NaN 9.0000
>> # B a new array, with 5 elements (A had 6 elements)
>> B = A(2:end)
B =
NaN 0.1200 NaN NaN 9.0000
>> # mask is a logical array with 5 elements, like B, and not 6, like A.
>> # mask knows nothing about A or B. It simply "selects" (indexes) the
>> # 1st, 3rd, and 4th element of any array.
>> mask = isnan (B)
mask =
1 0 1 1 0
>> # Invert the mask, now "selects" the 2nd and 5th element of any array.
>> not_mask = ! mask
not_mask =
0 1 0 0 1
>> # Return the 2nd and 5th element of A.
>> A(not_mask)
ans =
NaN NaN
我认为你对这种行为感到惊讶,因为你期望CCD_;记得";它来自CCD_ 4以索引右边的"0";区域";CCD_ 5。这并没有发生,它只是一个逻辑数组;记得";不知道它来自哪个数组(通常用于索引不同的数组(。
附带说明一下,并回答您的一个问题,Matlab的行为与Octave相同。
不管怎样,你所做的看起来有点奇怪,也许可以这样做:
A(! isnan (A))(2:end)
您只差一个。
你需要做AnnansA2 = A(~isnan(A(1:end)))
如果您只想返回最后两个非nan,请对结果进行如下索引;
ananIdxs = ~isnan(A)
AnnansA2 = A(ananIdxs(2:end))