Why the squeeze function squeeze 1x3 array to 1x3, but squeeze 1x1x1x3x1 to 3x1?
    7 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    raym
      
 am 17 Dez. 2024
  
    
    
    
    
    Kommentiert: Stephen23
      
      
 am 17 Dez. 2024
            >> size(squeeze(rand(1,1,1,3,1,1,1)))
ans =
3     1
>> size(squeeze(rand(1,1,3,1,1,1)))
ans =
3     1
>> size(squeeze(rand(1,3,1,1,1))) % this is the unexpected results. Why not 3 x 1
ans =
1     3
>> size(squeeze(rand(3,1,1,1)))
ans =
3     1
2 Kommentare
  Matt J
      
      
 am 17 Dez. 2024
				
      Bearbeitet: Matt J
      
      
 am 17 Dez. 2024
  
			Even if it did as you expect, it is rarely safe/robust to use squeeze(), because you rarely know when the leading dimension you want will reduce during some computation to a singleton.
Similar to eval(),  squeeze() is something one should strive to avoid. Use A(:) or shiftdim() instead.
  Stephen23
      
      
 am 17 Dez. 2024
				Like LENGTH, SQUEEZE is fundamentally unpredictable and best avoided.
Use RESHAPE or PERMUTE or SHIFTDIM instead.
Akzeptierte Antwort
  Steven Lord
    
      
 am 17 Dez. 2024
        If I recall correctly (this was before my time at MathWorks started) squeeze was introduced when we introduced arrays with more than 2 dimensions in MATLAB. Arrays in MATLAB have to have at least two dimensions (the size function with 1 input and 1 output is generally expected to return a vector with two or more elements, I think strange things can happen if you have a class whose overloaded size method returns a scalar or empty) and so squeezing out singleton dimensions that would leave two or more dimensions is reasonable. But if you squeeze a 1-by-3 vector, do you want it to become a 3-by-1 vector?
This edge case is documented on the squeeze documentation page, BTW. "If A is a row vector, column vector, scalar, or an array with no dimensions of length 1, then squeeze returns the input A." Since the trailing singleton dimensions are dropped when rand is determining what size output to return, rand(1,3,1,1,1) is a row vector and so squeeze returns it unchanged.
sz = size(rand(1,3,1,1,1))
0 Kommentare
Weitere Antworten (2)
  Walter Roberson
      
      
 am 17 Dez. 2024
        "because"
It was a design choice for the function. It leaves the array alone if ndims is 2 and otherwise removes all of the singular dimensions.
0 Kommentare
  Tony
 am 17 Dez. 2024
        
      Verschoben: Walter Roberson
      
      
 am 17 Dez. 2024
  
      There appears to be a heuristic involved that when there is only one non-singleton dimension, if it looks like the expansion of a row vector (the second dimensions is the non-singleton one), then squeeze the input into a row vector. Otherwise, squeeze into a column vector. Which from usage perspective, I agree with and believe is the more useful choice. When the non-singleton dimension is third or higher, then there's ambiguity about the more "useful" choice, and so the algorithm defaults to just column vector.
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





