do not regress if any Y is zero

조회 수: 3 (최근 30일)
aine gormley
aine gormley 2019년 2월 23일
편집: Matt J 2019년 2월 24일
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

답변 (1개)

Matt J
Matt J 2019년 2월 23일
How about
c = regress(y(y~=0), x(y~=0));
  댓글 수: 7
aine gormley
aine gormley 2019년 2월 24일
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 2019년 2월 24일
편집: Matt J 2019년 2월 24일
So, tell me again why can't you set the results to NaN?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by