What is the syntax for declaring a global 2-dimensional array in MATLAB?
I want the array to be blank, or uninitialized. That is, I want to be able to initialize it to some value later on using a for-loop. All the examples I have come across so far consist of initializing the array when it is declared. I find this rather tedious because my array might have to be a large one.
Thanks.
Declare a variable as global first before using it:
MATLAB doesn’t really support the concept of ‘uninitialised’ variables, but you can create an array of NaNs (not a number) to indicate that each value hasn’t been assigned yet. The arguments to the
nanfunction indicate the size of the NaN array you wish to create:There are other similar functions in case you wish to initialise arrays of zeros, ones, Inf etc.
Then inside the functions you want to use it in, declare it as global again:
As an aside, you note that you will “initialise it to some value later using a for-loop”. Depending on how you are initialising the array, there may be a vectorised way to achieve this (i.e. without using a for-loop). Vectorised operations are usually much quicker in MATLAB.