-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelTrend3D.m
More file actions
37 lines (31 loc) · 1.36 KB
/
Copy pathpixelTrend3D.m
File metadata and controls
37 lines (31 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function trend = pixelTrend3D(data)
%--------------------------------------------------------------------------
% pixelTrend3D computes the temporal trend for each pixel in a 3D array.
%--------------------------------------------------------------------------
% Input:
% data: 3D numeric array (latitude x longitude x time)
% Output:
% trend: 2D array (latitude x longitude) containing linear trends
%--------------------------------------------------------------------------
% Validate input data
if ndims(data) ~= 3
error("Input data must be a 3D matrix (latitude x longitude x time)");
end
% Get the size of the data
[rows, cols, ~] = size(data);
% Initialize the trend array
trend = NaN(rows, cols); % Use NaN to avoid uninitialized values
% Calculate the trend for each (i, j) position
for i = 1:rows
for j = 1:cols
% Extract the data for the current (i, j) position
current_data = squeeze(data(i, j, :));
% Check if sufficient non-NaN values exist for fitting
valid_idx = ~isnan(current_data);
if sum(valid_idx) > 1 % At least two valid points needed for polyfit
coefficients = polyfit(find(valid_idx), current_data(valid_idx), 1);
trend(i, j) = coefficients(1);
end
end
end
end