File Exchange

image thumbnail

ndgridVecs

version 1.0.0 (4.76 KB) by Matt J
A more efficient substitute for ndgrid meant to help capitalize on implicit expansion.

1 Download

Updated 10 Apr 2020

View License

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:

[I,J,K]=ndgrid(0:511);
X=I+J+K;

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,

[Iv,Jv,Kv]=ndgridVecs(0:511);
X=Iv+Jv+Kv;

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.

Cite As

Matt J (2020). ndgridVecs (https://www.mathworks.com/matlabcentral/fileexchange/74956-ndgridvecs), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)

MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux