loop not storing, or running through all values

6 次查看(过去 30 天)
I dont really know what im doing with MATLAB so cant work out whats wrong.
Im trying to make a loop that will use a function I have made to calculate the seasonal cycle and its range at each location within an area (latitude and longitude) :
function seasonal_cycle = calc_seasonal_cycle(sst,time)
nharm = 1;
year_length = 365.25;
cutoff = 0;
[r_sst,c_sst] = size(sst);
[r_time,c_time] = size(time);
if (r_sst > r_time)
sst = sst.' ;
end
ix_n = find(~isnan(sst));
if isempty(ix_n)
timeseries = NaN;
disp('Input data is all NaN')
end
if length(ix_n) < 2
seasonal_cycle = NaN;
disp('Input data mostly NaN')
end
sst_good = sst(ix_n);
time_good = time(ix_n);
[amplitude,phase,frac,offset,yy] = fit_harmonics(sst_good,time_good, nharm, year_length, cutoff); % using fit_harmonics function to fit cosine
seasonal_cycle = offset + amplitude .* cos(2 .* pi .* (time_g/year_length) + phase); % equation for cycle
end
loop:
[nr,nc,nt] = size(sst);
cycle_range = NaN(nr,nc);
for row = 1:1:nr
for col = 1:1:nc
[~,loop_lat_ix] = min(abs(lat - row));
[~,loop_lon_ix] = min(abs(lon - -col));
loop_timeseries = squeeze(sst(loop_lat_ix,loop_lon_ix,:));
loop_seasonal_cycle = calc_seasonal_cycle(loop_timeseries,time);
cycle_range (row,col) = max(loop_seasonal_cycle(:))-min(loop_seasonal_cycle(:));
end
end
this gives me a matrix where the first 60 colomns in the first row give a value, and every other row and column is NaN.
  2 个评论
chrisw23
chrisw23 2022-12-6
How about to use a conditional breakpoint in your loop to debug the problem yourself?
Manoj Mirge
Manoj Mirge 2023-2-22
Can you provide all the files used by your programm ?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by