逻辑阵列 - 在分配A(I)= B,B中元素的个数,我必须是相同的 [英] Logical Arrays - In an assignment A(I) = B, the number of elements in B and I must be the same
问题描述
我有三个矩阵,A,B和C.当B大于A,我想与A.饱和的值它说,在我的元素数量(这是(B> 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
在分配A(I)= B,B中的元素,我的号码必须是相同的。>> numel(B> A)ANS = 9>> numel(A)ANS = 9>> numel(A> B)ANS = 9
这也是奇怪的是,这个作品。
>> C(B< A)= AC = 5 5 5 5 5 5 5 5 5
我只是想通了......
C(B> A)= B(B> A)
块引用>
块引用>C =
5 5 5 5 5 5 5 5 5
解决方案的原因是因为
B> A
的永不满足,并产生空集。这会产生空矩阵([]
)。B
的每一个元素实际上的小比A
。因此,这等同于执行:C([])= A;
您正试图在矩阵分配
A
为无处,然后这些尺寸不匹配。之所以B< A
的工作原理是,因为B
的每一个值小于A
,所以分配A的
将在这里工作。一般情况下,你需要确保你正在访问的前pression右侧元素总数的必须等于在除权的左侧的相同数量的元素pression你想要的元素分配给正如你在评论中提到,这样做的:
C(B> A)= B(B> A)
将工作。这相当于这样做的:
C([])= B([]);
...基本上,您正在执行什么都没有,所以这是一个安全的操作。没有值在
正在访问B
被分配到的位置在A
。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 = [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 =
5 5 5 5 5 5 5 5 5
解决方案The reason why is because
B > A
is never satisfied, and produces the empty set. This will produce the empty matrix ([]
). Every element ofB
is actually smaller thanA
. 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. The reason whyB < A
works is because every value ofB
is less thanA
, and so the assignment ofA
will work here. 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 inA
.这篇关于逻辑阵列 - 在分配A(I)= B,B中元素的个数,我必须是相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!