loop not storing, or running through all values
7 views (last 30 days)
Show older comments
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 Comments
chrisw23
on 6 Dec 2022
How about to use a conditional breakpoint in your loop to debug the problem yourself?
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!