1x1矩阵的第一个元素与1x1矩阵的所有元素之间的差异 [英] Difference between first element of 1x1 matrix and all elements of 1x1 matrix
问题描述
在中找到一些逻辑之后处理了结构,我想检查一下如何将其推广到矩阵.
After finding some logic in how empty structs are dealt with, I wanted to check how this generalized to matrices.
在这里,我注意到以下内容:
Here I noticed the following:
如果您具有1x1矩阵,则将其分配给第一个元素.这与分配给所有元素不同.
If you have a 1x1 matrix, and assign to the first element. It is not the same as assigning to all elements.
这让我感到很惊讶,因为第一个元素实际上与本例中的所有元素相同.这是我的观察结果:
This rather surprised me as the first element is really the same as all the elements in this case. Here are my observations:
x = 1;
y = 1;
z = 1;
x(:) = []; % Evaluates to [] as I expected
y(1) = []; % Evaluates to Empty matrix: 1-by-0, rather than []
z(1,1) = []; %Throws an error: 'Subscripted assignment dimension mismatch.' even though size(z) gives [1 1];
z(1,:) = []; % Evaluates to Empty matrix: 0-by-1, just like z(:,:) = []
看到此问题后,我的问题是:
After seeing this my question is:
为什么以不同的方式分配给同一事物会导致四个不同的结果?
推荐答案
这似乎是一种一致性.
It just seems like a consistency thing.
让我们考虑一个更大的矩阵,看看行为是否与1
-by- 1
矩阵一致(在我看来,扰流板警报):
Lets consider a bigger matrix and see if the behaviour is consistent with a 1
-by-1
matrix (spoiler alert, it is in my opinion):
X = rand(3);
案例1:
X(1,1) = []
要使其正常工作是没有意义的.我们无法保留形状并放下单个元素,因此会出现尺寸不匹配误差,这与您的观察一致.尺寸失配也是一个适当的错误,因为我们试图将0
-by- 0
矩阵强制插入到1
-by- 1
插槽中. (顺便说一句,您说size(z)
给您[1 1]
,但size(z, 3)
也给您1
,size(z,7)
也给您size(z,7)
等,所以实际上它是一个[1 1 1 ...
矩阵)
It would make no sense for this to work. We can't preserve the shape and drop a single element hence we get a dimension mismatch error, which is consistent with your observation. Also dimension mismatch is an appropriate error as we're trying to force a 0
-by-0
matrix into a 1
-by-1
slot. (BTW on a side note you say size(z)
gives you [1 1]
but size(z, 3)
also gives you 1
and so does size(z,7)
etc so actually it's a [1 1 1 ...
matrix)
案例2:
X(1) = []
这将导致X
,使得size(X)
是1
-by- 8
,如果您指定线性索引,MATLAB似乎很乐意线性化您的矩阵.这对我来说很有意义,并且再次与1
-by- 1
情况一致,因为它会导致1
-by- numel(X)-1
矩阵(即1
-by- 0
对于X = 1
)
This results in an X
such that size(X)
is 1
-by-8
, MATLAB seems happy to linearize your matrix if you specify a linear index. This makes sense to me, and again is consistent with the 1
-by-1
case since it results in a 1
-by-numel(X)-1
matrix (i.e. 1
-by-0
for X = 1
)
案例3:
X(1,:) = []
这很简单,删除第一行,所以现在我们有了一个n-1
-by- m
矩阵.因此,3
-by- 3
变为2
-by- 3
,所以我对1
-by- 1
变为0
-by- 1
感到满意这个案例.请注意,X(:,1) = []
在相似的静脉中也是一致的.
That's pretty straight forward, remove the first row so now we have an n-1
-by-m
matrix. So a 3
-by-3
becomes a 2
-by-3
so I'm happy with a 1
-by-1
becoming a 0
-by-1
in this case. Note that X(:,1) = []
is also consistent in a similar vein.
案例4:
X(:) = []
这很有意义,重新分配整个矩阵.没有问题.没有歧义.
This one just makes sense, re-assign the entire matrix. No issues. No ambiguity.
因此,即使可以肯定,它们都可以毫无疑问地表示同一件事.我认为,对于MATLAB而言,与对单个元素矩阵进行相同操作相比,获得与大型矩阵一致的不同结果是完全合理的设计选择.
So even though sure, they all could unambiguously mean the same thing. I think it's a perfectly reasonable design choice for MATLAB to have different results that are consistent with larger matrices than always do the same thing for a single element matrix.
这篇关于1x1矩阵的第一个元素与1x1矩阵的所有元素之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!