For loop index assistance

I have these nested for loops. I'm trying to output these delta_K_estimate values but it keeps populating with the same values from 1:101, 102:202, 203:303, etc. Each value across all columns should vary and be different from the previous. I'm sure it's some small index error that i'm missing but can't see. Thanks in advance for you help!
P=zeros(length(transpose(K_hat)),2);
delta_K_estimate=zeros(82,505);
for i=0:number_of_peaks-1 %each peak
for j=(101*i)+1:(101*i)+101 %each C_avg
P(j,:)=polyfit(K_hat(:,j),delta_K(:,i+1),1); %determination of linear fit equation
delta_K_estimate(:,j)=P(j,1).*K_hat(:,j)+P(j,2); %linear fit estimate of delta_K
end
end

3 个评论

I don't think that's really necessary for answering a syntax/indexing error. I'm sure random data will suffice.
Well, knowing the sizes would certainly help even w/o specific data to be able to try to read the code without making up stuff.
NB:
P=zeros(length(transpose(K_hat)),2);
length(X) is defined as max(size(X)) so you may, or may not, have the result there that is expected. Use the proper size() dimension argument needed to ensure robust code.
delta_K_estimate(:,j)=polyval(P(j,:),K_hat(:,j)); %linear fit estimate of delta_K

请先登录,再进行评论。

回答(1 个)

I do not understand the reason you are indexing ‘j’ as a for loop.
I would do something like this to get consecutive ‘j’ vectors of length 100, beginning at 1, 101, 201, 301, ...
for i=1:number_of_peaks %each peak
j = (1:100)+100*(i-1);
P(j,:)=polyfit(K_hat(:,j),delta_K(:,i+1),1); %determination of linear fit equation
delta_K_estimate(:,j)=P(j,1).*K_hat(:,j)+P(j,2); %linear fit estimate of delta_K
end

类别

在 帮助中心 和 File Exchange 中查找有关 Descriptive Statistics and Insights 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by