loop not storing, or running through all values

조회 수: 6 (최근 30일)
Susannah
Susannah 2022년 12월 6일
댓글: Manoj Mirge 2023년 2월 22일
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