mean regression of variable in multiple nc files

2 views (last 30 days)
Please can someone advise on my code for Matlab. I am trying to do linear regression on the TXx variable across 5 netcdf files. I have multiple approaches to this but still getting error.
With the current code, the exact error is: "Error using regress Y must be a vector and must have the same number of rows as X."
(this is because the output latitude dimension of mean_TXx5s is 1 x 288 x 30 and it should be 192 x 288 x 30), so I can see why the error but I cannot seem to solve the problem.
NB: the dimensions of TXx (maximum temperature at each lat/lon at each timestep) are 288 x 192 x 30. Time is 30 years, timestep=1year.
% initialise time parameters
time_begin = [1981, 1, 1, 0,0,0];
time_end = [2010,12,31,23,0,0];
years = (time_begin(1):time_end(1))';
nyears = length(years);
% create storage and regress
TXx5s = [];
num_files = 5;
for i = 1 : num_files
TXx5 = randi(100, 288, 192, 30);
lat = rand(192, 1);
lon = rand(288, 1);
time = rand(30,1);
TXx5s = cat(4, TXx5s, TXx5);
end
M = numel(lon);
N = numel(lat);
T = numel(time);
x = ([ones(T, 1) years]);
mean_TXx5s = mean(TXx5s);
mean_TXx5s=permute(mean_TXx5s,[2,1,3,4]);
slope_TXx5 = zeros(M, N);
intercept_TXx5 = zeros(M, N);
for i = 1 : M
for j = 1 : N
y_TXx5 = squeeze(mean_TXx5s(i, j, :));
c = regress(y_TXx5, x);
intercept_TXx5(i, j) = c(1);
slope_TXx5(i, j) = c(2);
end
end

Answers (1)

Satoshi Kobayashi
Satoshi Kobayashi on 13 Feb 2019
mean_TXx5s = mean(TXx5s);
Function mean calculates the mean along lon.
mean_TXx5s = mean(TXx5s,4);

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!