MATLAB 2013a: total + measurement inconsistency

Please let me try to explain with 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 that I have to catch a special case when numel_last_x == 1 apply the transpose operation for consistency with the next steps. I suppose there should be a more elegant solution. Can you guys help me?

Edit: Sorry, the code was incorrect!

+5
source share
2 answers

The following keywords are here:

  • The inconsistency you are talking about is deeply immersed in the Matlab language: all arrays are considered at least 2D . For example, ndims(pi) gives 2 .
  • Another rule in Matlab is that it is assumed that all arrays have an infinite number of finite singleton sizes . For example, size(pi,5) gives 1 .

According to observation 1, squeeze will not remove singleton sizes if it gives less than two dimensions. This is stated in the documentation:

B = squeeze(A) returns an array of B with the same elements as A , but when deleting all singleton sizes. A singleton size is any dimension for which size(A,dim) = 1 . Two-dimensional arrays are not affected by squeeze ; if A is a row or column vector or scalar (1-on-1) value, then B = A

If you want to get rid of the first singlet, you can use observation 2 and use reshape

 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 
+5
source

You can use shiftdim instead of compression

 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 
+2
source

Source: https://habr.com/ru/post/1234163/


All Articles