For simplicity, I am taking A and B for which we want to find spatial correlation. lat have the dimension 240 and lon have the dimension 120.
Note: This method is applicable only if the A and B are in timesteps. For eg. dimesion of A and B are 120*240*365 . 365 is the timesteps.
If your data set contains some nan values, then you have to remove those nans for finding correlation, otherwise you will get all nan values If there is nan values, then use this
Below is function for nancorr2. Is you are using 2016b or later, then you can write function within a script. Otherwise you have to save it in another file.