简体   繁体   English

使用矩阵逻辑索引A(I)= B的Matlab错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM