[英]Matlab error using matrix logical indexing A(I) = B
Trying to do a matrix logical indexing, but Matlab seems to be crazy, what I'm missing here? 试图做矩阵逻辑索引,但是Matlab似乎很疯狂,我在这里缺少什么?
Look: 看:
>> A
A =
NaN NaN NaN NaN
>> B
B =
1 2 3 4
>> I
I =
1 1 0 0
>> A(I) = B
In an assignment A(I) = B, the number of elements in B and I must
be the same.
>> numel(B)
ans =
4
>> numel(A)
ans =
4
Rest assured, MATLAB isn't crazy. 放心,MATLAB并不疯狂。
B
has 4 elements as you've shown and A(I)
only has 2 because I
only has 2 true
values. 正如您所显示的,
B
有4个元素,而A(I)
只有2个元素,因为I
只有2个true
值。
numel(B)
% 4
numel(A(I))
% 2
You are trying to assign 4 elements into two elements in A
is which is exactly what your error states. 您试图将4个元素分配给
A
两个元素,这正是您的错误状态。
In an assignment A(I) = B, the number of elements in B and I must be the same
在分配A(I)= B中,B和I中的元素数必须相同
You can only assign two values from B
into A
with your specific value if I
. 如果
I
则只能使用您的特定值将B
两个值分配给A
I'm guessing you would want to only assign the corresponding elements of B
like this. 我猜想您只想这样分配
B
的对应元素。
A(I) = B(I);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.