loop not storing, or running through all values

7 visualizaciones (últimos 30 días)
Susannah
Susannah el 6 de Dic. de 2022
Comentada: Manoj Mirge el 22 de Feb. de 2023
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 comentarios
chrisw23
chrisw23 el 6 de Dic. de 2022
How about to use a conditional breakpoint in your loop to debug the problem yourself?
Manoj Mirge
Manoj Mirge el 22 de Feb. de 2023
Can you provide all the files used by your programm ?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by