arraysmatlabmatrixmultidimensional-array

Squeeze Some of Singleton Dimensions in Matlab


How can I squeeze only a subset of singleton dimensions of a matrix in Matlab? The squeeze function removes them all.

I keep the index to those dimensions in a vector called "dims".


Solution

  • Code

    %// Input matrix is assumed as A
    sz = size(A)
    t2 = sz~=1
    t2(dims)=1
    out = reshape(A,sz(t2)) %// out is the desired output
    

    If you are crazy about dense codes, you can try this -

    sz = size(A)
    out = reshape(A,sz(sort([dims find(sz~=1)])))