Double checking a for loop

Can someone just double check my for loop. Specifically the bolded line. I wanted to calculate the integrated chlorophyll and then use it to find the relative amount by dividing temp by the intchl. Technically speaking, if I add up all my relative chlorophyll values for each column they should equal 1. However, they are coming out totalling a little over one so I think I messed up.
for i=1:length(databin)
temp=databin{i};
temp=temp(:,Varnum);
VARfield(1:length(temp),i)=temp;
intchl(i) = nansum(0.5.*(temp(1:end -1) + temp(2:end))); %MARKED
%integrated chlorophyll should just be one number for each cast
relchl = temp/intchl(i);
VARfieldrel(1:length(relchl),i)=relchl;
end

回答(1 个)

Sergey Kasyanov
Sergey Kasyanov 2021-3-13

0 个投票

Hello,
sum(temp/intchl) -> sum(temp)/intchl means that you divide sum of all values by sum of mean values. Lets write it in detail:
sum(temp) = x1 + x2 + x3 + ... + x(n-1) + x(n)
intchl = (x1+x2)/2 + (x2 + x3)/2 + ... + (x(n-2) + x(n-1)/2 + (x(n-1) + xn)/2 = x1/2 + x2 + x3 + .... + x(n-1) + xn/2 = sum(temp) - (temp(1) + temp(end))/2
It means that intchl is lower than sum(temp).
I think you should to calculate intchl as sum(temp).

6 个评论

Do you need to integrate temp over the bincenters?
In that case you can use next code:
intchl(i) = nansum((temp(1:end-1) + temp(2:end))/2.*diff(bincenters'))
actually I think I do. because the data is binned by the depth which is the bincenters. That might work!
Sergey, my bincenters is 1 by 20 .. I still get the error, "matrix dimensions don't agree".
VARfield=NaN(totalrow(1),length(databin));
Varnum=11; %chloro
for i=1:length(databin)
temp=databin{i};
temp=temp(:,Varnum);
VARfield(1:length(temp),i)=temp;
%intchl(i) = nansum((temp(1:end-1) + temp(2:end)))
intchl(i) = nansum((temp(1:end-1) + temp(2:end))/2.*diff(bincenters'));
%integrated chlorophyll should just be one number for each cast
relchl = temp/intchl(i);
VARfieldrel(1:length(relchl),i)=relchl;
end
Sergey I'm stuck here and bolded line still gives me the error 'Matrix dimensions don't agree'
I cant understand why error still exist. Maybe you should to use diff(bincenters) instead of diff(bincenters')?
databin= cell(length(datafi),1)
for i = 1:length(datafi);
%for i =1;
temp=datafi{i};
minval = 0;
maxval = max(temp.Var7);
step = 0.25; %increase to 25, was at 0.10
bins = minval:step:maxval;
bincenters = bins + step/2;
bincenters = bincenters(1:length(bincenters)-1);
DATAavg_input = ones(length(bincenters),size(temp,2)).*NaN;
for xx = 2:length(bins);
nn = find(temp.Var7 >= bins(xx-1) & temp.Var7 < bins(xx));
DATAavg_input(xx-1,3:end) = nanmean(table2array(temp(nn,3:end)),1);
end
DATAavg_input(:,1)= datenum(datetime(temp.Var1(1), 'InputFormat', 'MM/dd/yyyy', 'Format', 'MM-dd-yyyy hh:mm:ss.SSSS') + temp.Var2(1));
%make sure to convert it back for contour using datetime
databin{i}=DATAavg_input;
end
DataBin = DATAavg_input;
Sergey, for the time being do you know how to convert back the time from the bolded line?? I tried using xticks later on in my script and realized all the date and times were the same so xtick was saying 00:00 for everything??

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by