Please let me try to explain by an example
请让我试着通过一个例子来解释
numel_last_a = 1;
numel_last_b = 2
a = rand(2,20,numel_last_a);
b = rand(2,20,numel_last_b);
size(squeeze(sum(a,1)))
size(squeeze(sum(b,1)))
in this case, the output will be
在这种情况下,输出将是
ans = 1 20
ans = 20 2
This means I have to catch the special case where numel_last_x == 1 to apply a transpose operation for consistency with later steps. I'm guessing that there must be a more elegant solution. Can you guys help me out?
这意味着我必须捕获numel_last_x == 1的特殊情况,以应用转置操作以与后续步骤保持一致。我猜想必须有一个更优雅的解决方案。你们能帮助我吗?
Edit: sorry, code was wrong!
编辑:对不起,代码错了!
2 个解决方案
#1
5
The following observations are key here:
以下观察结果如下:
- The inconsistency you mention is buried deep into the Matlab language: all arrays are considered to be at least 2D. For example,
ndims(pi)
gives2
. - Another rule in Matlab is that all arrays are assumed to have infinitely many trailing singleton dimensions. For example,
size(pi,5)
gives1
.
你提到的不一致性深埋在Matlab语言中:所有数组都被认为至少是2D。例如,ndims(pi)给出2。
Matlab中的另一个规则是假设所有数组都具有无限多个尾随单例维度。例如,size(pi,5)给出1。
According to observation 1, squeeze
won't remove singleton dimensions if doing so would give less than two dimensions. This is mentioned in the documentation:
根据观察1,如果这样做将少于两个维度,则挤压将不会移除单个维度。这在文档中提到:
B = squeeze(A)
returns an arrayB
with the same elements asA
, but with all singleton dimensions removed. A singleton dimension is any dimension for whichsize(A,dim) = 1
. Two-dimensional arrays are unaffected bysqueeze
; ifA
is a row or column vector or a scalar (1-by-1) value, thenB = A
.B = squeeze(A)返回一个数组B,其元素与A相同,但删除了所有单例尺寸。单个维度是尺寸(A,dim)= 1的任何维度。二维阵列不受挤压的影响;如果A是行或列向量或标量(1乘1)值,则B = A.
If you want to get rid of the first singleton, you can exploit observation 2 and use reshape
:
如果你想摆脱第一个单身,你可以利用观察2并使用重塑:
numel_last_a = 1;
numel_last_b = 2;
a = rand(2,20,numel_last_a);
b = rand(2,20,numel_last_b);
as = reshape(sum(a,1), size(a,2), size(a,3));
bs = reshape(sum(b,1), size(b,2), size(b,3));
size(as)
size(bs)
gives
ans =
20 1
ans =
20 2
#2
2
You could use shiftdim instead of squeeze
你可以使用shiftdim而不是挤压
numel_last_a = 1;
numel_last_b = 2;
a = rand(2,20,numel_last_a);
b = rand(2,20,numel_last_b);
size(shiftdim(sum(a,1)))
size(shiftdim(sum(b,1)))
ans =
20 1
ans =
20 2
#1
5
The following observations are key here:
以下观察结果如下:
- The inconsistency you mention is buried deep into the Matlab language: all arrays are considered to be at least 2D. For example,
ndims(pi)
gives2
. - Another rule in Matlab is that all arrays are assumed to have infinitely many trailing singleton dimensions. For example,
size(pi,5)
gives1
.
你提到的不一致性深埋在Matlab语言中:所有数组都被认为至少是2D。例如,ndims(pi)给出2。
Matlab中的另一个规则是假设所有数组都具有无限多个尾随单例维度。例如,size(pi,5)给出1。
According to observation 1, squeeze
won't remove singleton dimensions if doing so would give less than two dimensions. This is mentioned in the documentation:
根据观察1,如果这样做将少于两个维度,则挤压将不会移除单个维度。这在文档中提到:
B = squeeze(A)
returns an arrayB
with the same elements asA
, but with all singleton dimensions removed. A singleton dimension is any dimension for whichsize(A,dim) = 1
. Two-dimensional arrays are unaffected bysqueeze
; ifA
is a row or column vector or a scalar (1-by-1) value, thenB = A
.B = squeeze(A)返回一个数组B,其元素与A相同,但删除了所有单例尺寸。单个维度是尺寸(A,dim)= 1的任何维度。二维阵列不受挤压的影响;如果A是行或列向量或标量(1乘1)值,则B = A.
If you want to get rid of the first singleton, you can exploit observation 2 and use reshape
:
如果你想摆脱第一个单身,你可以利用观察2并使用重塑:
numel_last_a = 1;
numel_last_b = 2;
a = rand(2,20,numel_last_a);
b = rand(2,20,numel_last_b);
as = reshape(sum(a,1), size(a,2), size(a,3));
bs = reshape(sum(b,1), size(b,2), size(b,3));
size(as)
size(bs)
gives
ans =
20 1
ans =
20 2
#2
2
You could use shiftdim instead of squeeze
你可以使用shiftdim而不是挤压
numel_last_a = 1;
numel_last_b = 2;
a = rand(2,20,numel_last_a);
b = rand(2,20,numel_last_b);
size(shiftdim(sum(a,1)))
size(shiftdim(sum(b,1)))
ans =
20 1
ans =
20 2