I am currently using squeeze to remove two singleton dimensions from a matrix. The matrix is a large 4d matrix M(:,:,:,:). The first two dimensions are row and column coordinates (y and x). The variable in the third dimension (indexes) selects several values in the third dimension of M.
In a for-loop i am running, matrix M is adressed as M(y,x,indexes,:), which makes the first two dimensions singleton dimensions. These dimensions are then removed with squeeze for use in pdist, like so:
pdist(squeeze(M(y,x,indexes,:)))
Can i vectorize the use of squeeze in this case? (It takes up a lot of time in the loop)
If matrix
Mis not changed inside the loop, a simple solution is to reorder the matrix dimensions with PERMUTE before running for-loop:Then you can address
Mperminstead ofMasMperm(:,:,y,x).