Is there a way to vectorize the use of compression in matlab?

I am currently using squeeze to remove two singleton sizes from a matrix. The matrix is ​​a large 4d matrix M(:,:,:,:) . The first two dimensions are the coordinates of the rows and columns ( y and x ). A variable in the third dimension ( indexes ) selects several values ​​in the third dimension M

In the loop that I run, the matrix M is addressed as M(y,x,indexes,:) , which makes the first two dimensions one-dimensional. These sizes are then removed using squeeze for use in pdist , for example:

 pdist(squeeze(M(y,x,indexes,:))) 

Is it possible to vectorize the use of squeeze in this case? (It takes a lot of time in the loop)

+4
source share
2 answers

If the matrix M does not change inside the loop, a simple solution is to resize the matrix using PERMUTE before running for -loop:

 Mperm = permute(M,[3 4 1 2]); 

You can then address Mperm instead of M as Mperm(:,:,y,x) .

+4
source

For this type of problem using reshape there is often a significant improvement in compression. I had a problem where the compression took about half the time it took to execute the function. Using the profiler, I saw how compression does some unnecessary checks. Using reshape reduced the time for the same operation to 15% of the required initial time.

+1
source

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


All Articles