简体   繁体   English

是否有任何机制可以自动挤压 Matlab / Octave

[英]Is there any mechanism to auto squeeze in Matlab / Octave

For an nD array, it would be nice to be able to auto squeeze to remove singleton dimensions.对于 nD 数组,能够自动压缩以删除 singleton 尺寸会很好。 Is there a way to do this that I don't know about?有没有办法做到这一点,我不知道? This would be especially useful for aggregate functions (eg sum, mean, etc) where you always expect a result with fewer dimensions.这对于聚合函数(例如求和、平均值等)特别有用,在这些函数中,您总是期望得到具有较少维度的结果。

Here's a simple example:这是一个简单的例子:

>> A = ones(3,3,3);
>> B = mean(A);
>> size(B)
ans =  
   1   3   3

>> squeeze(B)
ans =
   1   1   1
   1   1   1
   1   1   1

It would be nice if Matlab/Octave would automatically do the squeezing for me.如果 Matlab/Octave 会自动为我进行挤压,那就太好了。 Or if there was a way to turn that option on (something similar to hold on for plots).或者,如果有办法打开该选项(类似于hold on的情节)。

As far as I know, Matlab does not have that.据我所知,Matlab没有这个。 And I don't think it would be a good idea.而且我认为这不是一个好主意。 Consider a modified version of your example:考虑您的示例的修改版本:

>> A = ones(3,1,1,3);
>> B = mean(A);
>> size(B)
ans =
     1     1     1     3

What should "auto-squeeze" do here? “自动挤压”应该在这里做什么? Reduce B to size [1 1 3] or to [1 3] ?B减小到[1 1 3][1 3]的大小?

  • You could argue that it should remove the same dimension that mean has turned into a singleton.您可能会争辩mean ,它应该删除与已变成 singleton 的相同维度。 But then it would have to be done within the mean function, perhaps with an optional input argument.但是,它必须在mean function完成,可能带有可选的输入参数。 Once you get the function output, there is no information how it was obtained.一旦您获得 function output,就没有关于它是如何获得的信息。

  • Or you could argue that it should remove all singleton dimensions, like squeeze (more or less) does.或者您可以争辩说它应该删除所有singleton 尺寸,就像squeeze (或多或少)一样。 But then it would remove dimensions that were already singleton in the function input , which is probably unwanted.但随后它将删除 function输入中已经是 singleton 的尺寸,这可能是不需要的。

If you ask me, having a second input in squeeze specifiyng which (singleton) dimensions to remove would be a nice addition (in the same vein as you can use mean(A, 1) to force the operation to be applied along the first dimension even if A happens to be a row vector).如果您问我,在squeeze中指定要删除哪些(单个)维度的第二个输入将是一个很好的补充(与您可以使用mean(A, 1)强制沿第一个维度应用操作相同)即使A恰好是一个行向量)。

I agree with Luis and Cris, but I would add the following.我同意 Luis 和 Cris 的观点,但我会补充以下内容。

Both Matlab and Octave do automatically squeeze extra dimensions, in a very particular scenario: any dimensions at the end that have been reduced to singletons, are automatically squeezed out. Matlab 和 Octave都会自动挤压额外的尺寸,在一个非常特殊的情况下:在最后被缩减为单件的任何尺寸都会被自动挤压出来。

Eg例如

A = ones([1,2,3,4]);
B = mean(A, 4);
size(B)
% ans = 1   2   3

Note, how the answer is [1,2,3] , and not [1,2,3,1] .请注意,答案是[1,2,3] ,而不是[1,2,3,1] This is in contrast to languages like python, for instance, where a size of (1,1) is very different to a size of (1,) .这与 python 之类的语言形成对比,例如, (1,1) 的大小与(1,)的大小非常不同。

Therefore, with regard to your questions, one way to use this to your advantage could be to ensure that the dimension that is to be reduced is always found at the end, and thus automatically simplified.因此,关于您的问题,利用这一点对您有利的一种方法可能是确保始终在最后找到要减少的维度,从而自动简化。

This becomes even more useful when you realise that:当您意识到:

size(A(:))       % ans =   24    1     (i.e. 24)
size(A(:,:))     % ans =    1   24
size(A(:,:,:))   % ans =    1    2   12
size(A(:,:,:,:)) % ans =    1    2    3    4

Meaning, if you order your dimensions hierarchically you can ensure that any operations that need to take place over the higher dimensions, can a) be vectorised easily, and b) give a natural result, without the need to waste time squeezing or permuting the resulting dimensions.意思是,如果您对维度进行分层排序,您可以确保任何需要在更高维度上进行的操作,都可以 a) 轻松矢量化,b) 给出自然结果,而无需浪费时间压缩或排列结果方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM