Say if I have a 5D Array with size 1024x1024x1x1x100. How can I make a new array that is 1024x1024x100?
The following works if you know which dimensions you want to keep ahead of time:
arr = arr[:, :, 1, 1, :]
But I don't know which dimensions are what size ahead of time and I would like to only keep dimensions given a boolean mask; something like this...
arr2 = arr[(size(arr) .> 1)]
squeeze function was defined specifically for the purpose of removing dimensions of length 1. From the manual:
Base.squeeze — Function.
Remove the dimensions specified by dims from array A. Elements of dims must be unique and within the range
size(A,i)must equal 1 for all
To "squeeze" all the dimensions of size 1 (when they are unknown in advance), we need to find them and make them into a tuple. This is accomplished by
((size(arr).==1)...). So the result is:
Thanks to a comment by Olivier, here is an:
dropdims is now (Julia 1.9 as of writing) the preferred way to squeeze out size 1 dimensions. For example, to drop all the size 1 dimensions from M:
If the dimensions are known, then it might be better to specify them explicitly instead of using the above