This submission contains a very simple bit of code, but one that is quite useful for taking advantage of Matlab's implicit expansion capabilities, introduced in R2016b. In the old days, one might have used ndgrid to do something like the following:
In the above, I,J,K, and X are all 512x512x512 arrays. This consumes a fair amount of memory, in fact 4 times the amount of memory consumed by the desired result X. Considerable CPU time is also spent allocating memory for and populating the grid arrays I, J, and K.
Then implicit expansion was introduced in R2016b allowing us to get the same result when I, J, and K are instead appropriately oriented "grid-vectors". The function ndgridVecs in this FEX submission is a tool to help generate such vectors. Just like ndgrid, it takes the desired grid vectors (in any orientation) as input, but reshapes/reorients them so that implicit expansion can be appropriately capitalized upon. With this function, the same array X as above can be generated as follows,
except that in this case the computation is based on vectors Iv, Jv, and Kv each of length 512. This is obviously much more efficient than the ndgrid method in which I,J,and K are 512x512x512 grid arrays.
See Examples tab for more detailed demonstration.
Matt J (2022). ndgridVecs (https://www.mathworks.com/matlabcentral/fileexchange/74956-ndgridvecs), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!