简体   繁体   English

逻辑数组 - 在赋值A(I)= B中,B和I中的元素数必须相同

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

相关问题 在赋值 A(:) = B 中,A 和 B 中的元素数量必须相同 - In an assignment A(:) = B, the number of elements in A and B must be the same 给定数字X,在两个排序的数组中找到两个元素,例如A [i] + B [j] = X在O(n + m)中 - Given a number X , find two elements in two sorted arrays such A[i]+B[j] = X in O(n+m) 给定3个排序的数组A,B,C和数字S,找到i,j,k,使得A [i] + B [j] + C [k] = S - Given 3 sorted arrays A, B, C and number S, find i, j, k such that A[i] + B[j] + C[k] = S 给定两个数组找到最小化和A [i] * | B [i] -B [k] |的索引k - Given two arrays find the index k that minimizes the sum A[i]*|B[i]-B[k]| 我的算法中的缺陷在哪里找出是否存在两个数组A,B的排列,使得它们具有(A [i] + B [i])&gt; = k - Where is the flaw in my algorithm to find whether there exists a permutation of two arrays A,B such that they have (A[i]+B[i]) >= k 老实说很难解释..我使用类似a = b的东西,然后使用a ++,但是b会改变。 除了这些是数组 - Honestly hard to explain.. I use something like a=b, then a++, but b changes. Except, these are Arrays c#数组b的多个3个元素 - c# arrays multiple 3 elements of array b 给定一个数字 N 和 2 arrays 大小为 N 的排序顺序的 A 和 B,打印公共元素。如果找不到,打印 -1 - given a number N and 2 arrays A and B of sorted order of size N, print the common elements.If it is not found print -1 如果 B 是 A 的子集,则从 a 中删除 B,其中元素的顺序相同 - If B is a subset of A, remove B from a, with elements in the same order Matlab错误A(I)= B - Matlab Error A(I) = B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM