[英]Logical Arrays - In an assignment A(I) = B, the number of elements in B and I must be the same
I have three matrices, A, B and C. When B is larger than A, I want to saturate the value with A. It says that the number of elements in I (which is (B > A)) must be the same as the number of elements in A. I checked below and they are the same. 我有三个矩阵,A,B和C.当B大于A时,我想用A饱和该值。它表示I中的元素数(即(B> A))必须与A中的元素数量。我在下面查看它们是相同的。
>> A = [5 5 5; 5 5 5; 5 5 5];
>> B = [2 2 2; 2 2 2; 2 2 2];
>> C(B > A) = A
In an assignment A(I) = B, the number of elements in B and I must be the same.
>> numel(B > A)
ans =
9
>> numel(A)
ans =
9
>> numel(A>B)
ans =
9
It is also strange that this works. 这也很奇怪。
>> C(B < A) = A
C =
5 5 5 5 5 5 5 5 5
I just figured it out... 我只想出来......
C(B>A) = B(B>A) C(B> A)= B(B> A)
C = C =
5 5 5 5 5 5 5 5 5
The reason why is because B > A
is never satisfied , and produces the empty set. 原因是因为B > A
永远不会满足 ,并产生空集。 This will produce the empty matrix ( []
). 这将产生空矩阵( []
)。 Every element of B
is actually smaller than A
. B
每个元素实际上都小于 A
As such, this is equivalent to performing: 因此,这相当于执行:
C([]) = A;
You are trying to assign A
to nowhere in the matrix, and those dimensions don't match. 您试图在矩阵中将A
指定为无处 ,并且这些维度不匹配。 The reason why B < A
works is because every value of B
is less than A
, and so the assignment of A
will work here. B < A
起作用的原因是因为B
每个值都小于A
,因此A
的赋值将在此处起作用。 In general, you need to make sure that the total number of elements you are accessing on the right side of the expression must equal the same number of elements on the left hand side of the expression you want to assign the elements to. 通常,您需要确保在表达式右侧访问的元素总数必须等于要为其分配元素的表达式左侧的相同元素数。
As you have mentioned in your comments, doing: 正如您在评论中提到的那样:
C(B > A) = B(B > A)
will work. 将工作。 This is equivalent to doing: 这相当于:
C([]) = B([]);
... essentially, you are performing nothing, so this is a safe operation. ......基本上,你什么都不做,所以这是一个安全的操作。 No values are being accessed in B
are being assigned to locations in A
. B
中没有访问的值被分配给A
位置。
B>A is a logical 3x3 matrix, but C(B>A) is the empty set, and you are assigning a 3x3 matrix to it. B> A是逻辑3x3矩阵,但C(B> A)是空集,并且您正在为其分配3x3矩阵。 Thus the error. 因此错误。 Try 尝试
C(B>A)=A(B>A);
On the other hand, C(B < A) is C, a 3x3 matrix, so C(B < A) is C, to which you can assign A. 另一方面,C(B <A)是C,一个3x3矩阵,所以C(B <A)是C,你可以指定A.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.