How to find correlations between a vector and a 3D array and plot the correlations spatially?

7 views (last 30 days)
How can I calculate correlations between a vector of isotope data from a single location and a spatial array of precipiation data? And how can I plot the correlations for visual interpretations?
Here are some fake data:
isotope = rand(1,50)*10; % 1x50 vector of isotopes/year
lat = linspace(1,60,120); % 1x120 vector of latitudes
lon = linspace(120,180,120); % 1x120 vector of longitudes
time = 1901:1950; % 1x50 vector of years
precip = randi(3000,120,120,50); % 120x120x50 array of precipitation/year
Thanks!

Accepted Answer

the cyclist
the cyclist on 14 Sep 2021
Is it correct that the result will be a 120x120 array of correlations, where each entry is the correlation of the 50 isotope values (which never change), and the 50 precip values at that location.
And is it also correct that we do not actually need the lat, lon, and time values to calculate that?
If so, then I believe this does what you want. I tried to fully comment the code, so you could more easily understand what is going on.
% Set random number generator seed, for reproducibility
rng default
% The data
isotope = rand(1,50)*10; % 1x50 vector of isotopes/year
lat = linspace(1,60,120); % 1x120 vector of latitudes
lon = linspace(120,180,120); % 1x120 vector of longitudes
time = 1901:1950; % 1x50 vector of years
precip = randi(3000,120,120,50); % 120x120x50 array of precipitation/year
% Preallocate memory for the correlations
r = zeros(120,120);
% Loop over the locations
for xi = 1:120
for yi = 1:120
r_mat = corrcoef(isotope,precip(xi,yi,:)); % This gets all pairwise correlations
r(xi,yi) = r_mat(1,2); % Store only the cross-correlation (at this location)
end
end
% Heatmap of the correlations
figure
imagesc(r)
axis square
  1 Comment
Austin M. Weber
Austin M. Weber on 14 Sep 2021
This is excellent! I appreciate you for leaving comments with explanations. They were very helpful in my understanding. Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!