Nested loop for matrix

1 回表示 (過去 30 日間)
Mitali Murkar
Mitali Murkar 2022 年 12 月 15 日
コメント済み: Voss 2022 年 12 月 17 日
Below is my code. I want to calculate the value of g where my for loop iterates for i and K. It means I hould have 6 combinations of output.However, I am only getting 3 combinations of output saved in g. Could you please provid the hint or feeback on it? Thanks!
clc
clear all;
ED = readmatrix("Data filteration1.xlsx");
ki = 0.0042; %%current temp coeff
Ns=72; %%72cells in series and 1 in parellel
Np=1;
Ego=1.1;
K= 1.380*10^-23;
q= 1.602*10^-19;
Voc=48.37;
Isc=10.27;
G=[1000 800];
Tck = [25+273 20+273];
I=zeros(400,1);
Vi=ones(400,1);
X= [1.53 0.35 700;1 0.3 600;1.1 0.33 750];
x3=X(:,1);
x4=X(:,2);
x5=X(:,3);
for i=1:length(Tck) % I=2
for k=1:size(X,1) %k=3
Vt(k,i) = K.*Tck(i)/q;
Iph(k,i)= (Isc + ki .* (Tck(i)-298)).*(G(i)./1000);
Irs(k,i) = Isc./((exp((q.*Voc)./(x3(k,:).*Ns.*K.*Tck(i))))-1);
Io(k,i) = Irs(k,i).* (Tck(i)./298).^3 .* exp(((q.*Ego)./(x3(k,:).*K))*(1./298 - 1./Tck(i)));
g(k,:) = (Np.*Iph(k,i))-Io(k,i).*(exp((Vi+I.*x4(k,:))./Vt(k,i)/Ns./x3(k,:))-1)-((Vi+ I.*x4(k,:))./x5(k,:)) - I;
end
end

採用された回答

Voss
Voss 2022 年 12 月 15 日
clc
clear all;
% ED = readmatrix("Data filteration1.xlsx");
ki = 0.0042; %%current temp coeff
Ns=72; %%72cells in series and 1 in parellel
Np=1;
Ego=1.1;
K= 1.380*10^-23;
q= 1.602*10^-19;
Voc=48.37;
Isc=10.27;
G=[1000 800];
Tck = [25+273 20+273];
I=zeros(400,1);
Vi=ones(400,1);
X= [1.53 0.35 700;1 0.3 600;1.1 0.33 750];
x3=X(:,1);
x4=X(:,2);
x5=X(:,3);
nT = length(Tck);
nX = size(X,1);
g = zeros(nT*nX,numel(I));
row = 1;
for i=1:nT % I=2
for k=1:nX %k=3
Vt(k,i) = K.*Tck(i)/q;
Iph(k,i)= (Isc + ki .* (Tck(i)-298)).*(G(i)./1000);
Irs(k,i) = Isc./((exp((q.*Voc)./(x3(k,:).*Ns.*K.*Tck(i))))-1);
Io(k,i) = Irs(k,i).* (Tck(i)./298).^3 .* exp(((q.*Ego)./(x3(k,:).*K))*(1./298 - 1./Tck(i)));
g(row,:) = (Np.*Iph(k,i))-Io(k,i).*(exp((Vi+I.*x4(k,:))./Vt(k,i)/Ns./x3(k,:))-1)-((Vi+ I.*x4(k,:))./x5(k,:)) - I;
row = row+1;
end
end
size(g)
ans = 1×2
6 400
  2 件のコメント
Mitali Murkar
Mitali Murkar 2022 年 12 月 17 日
Hello Voss, thank you so much for the hint. It works now! Very helpful.
Voss
Voss 2022 年 12 月 17 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2022 年 12 月 15 日
I'm guessing you intended
g(k,i)
rather than
g(k,:)
in the last line of the for loop.
  3 件のコメント
the cyclist
the cyclist 2022 年 12 月 15 日
I'm not certain why you think it should be 6*400. If you look at your indexing, here is what happens during all the iterations of your for loops:
i==1, k==1
Write g(1,:)
i==1, k==2
Write g(2,:)
i==1, k==3
Write g(3,:)
i==2, k==1
Over-write to g(1,:)
i==2, k==2
Over-write to g(2,:)
i==2, k==3
Over-write to g(3,:)
===========================
Notice that you never use i to index into g, so you are overwriting instead of creating rows 4-6.
Mitali Murkar
Mitali Murkar 2022 年 12 月 17 日
Thank you so much for clarification and your very prompt response.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by