do not regress if any Y is zero

Hi, I am using regress to perform linear regression on a large nc file (containing lat, lon, time, and temperature). So for each lat, lon, temperature is regressed over 30 years (one measurement each year).
In some years, some temperatures are 0 K, in those cases I want the loop to not regress, so the final mean slope does not include these.
Here is the important part of my code:
I am not getting an error but also not getting any difference in whether i include y(y==0) = NaN;, so I think there may be a better way. I had understood that regress ignores these autmatically, but that also appears to not be the case.
% 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
TXx = randi(100, 288, 192, 30);
lat = rand(192, 1);
lon = rand(288, 1);
time = rand(30,1);
M = numel(lon);
N = numel(lat);
slope = zeros(M, N);
intercept = zeros(M, N);
T = numel(time);
x = ([ones(T, 1) years]);
% Regress each lat/lon location
for i = 1 : M
for j = 1 : N
% Get all time instances of each lat/lon location
y = squeeze(TXx(i, j, :));
y(y==0) = NaN;
% Create regression problem and solve
c = regress(y, x);
intercept(i, j) = c(1);
slope(i, j) = c(2);
end
end

Réponses (1)

Matt J
Matt J le 23 Fév 2019
How about
c = regress(y(y~=0), x(y~=0));

7 commentaires

aine gormley
aine gormley le 23 Fév 2019
Modifié(e) : aine gormley le 23 Fév 2019
Hi, thanks for your comment. I think then though then matlab just excludes data if it is zero and regresses the rest. But is there a way to exclude an entire timeline if one of the timesteps is zero?
For example, at a certain lat/lon, in 1999 - the temperature is 0, is there a way to exclude that entire lat/lon from the regression?
aine gormley
aine gormley le 23 Fév 2019
Just wondering perhaps your solution works - but now i get the error:
Index exceeds the number of array elements (1).
Error in ensmean2d (line 43)
slope(i, j) = c(2);
but i am unsure if i can change that?
Matt J
Matt J le 23 Fév 2019
Modifié(e) : Matt J le 23 Fév 2019
You need to decide how you want things handled when c is a scalar. I assume this happens when only one of the y's is non-zero.
aine gormley
aine gormley le 23 Fév 2019
Modifié(e) : aine gormley le 23 Fév 2019
can a slope (i.e. the slopes within which some y's are zero) be replaced by NaN at that lat/lon?
because i then plot the slopes on the grid so if I compress the slopes/have the non-zeros a removed it wont plot.
Matt J
Matt J le 24 Fév 2019
If only one y is non-zero there is no way to derive a slope. You cannot fit a line when there is fewer than 2 data points.
aine gormley
aine gormley le 24 Fév 2019
Hi, sorry - I meant if only 1 y is zero (so the other 29 are not zero), do you know if there a way to exclude this regression in such cases?
Matt J
Matt J le 24 Fév 2019
Modifié(e) : Matt J le 24 Fév 2019
So, tell me again why can't you set the results to NaN?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2018b

Modifié(e) :

le 24 Fév 2019

Community Treasure Hunt

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

Start Hunting!

Translated by