Iterate over two arrays

조회 수: 36 (최근 30일)
Ryan Majid
Ryan Majid 2022년 8월 21일
답변: Image Analyst 2022년 8월 21일
Hi,
I would like to write a code whith two arrays as inputs: W_C and L_C
I want to calculate some formulas for each value. for instant:
W_C=[ 1 2 3 4]
L_C=[ 5 6 7 8]
I want to calculate all for mulas when:
W_C = 1 and L_C=[5 6 7 8]
W_C= 2 and L_C=[5 6 7 8]
.
.
W_C=4 and L_C=[5 6 7 8]
So, I should have 16 results. Here is a code I'm thinking of. however, I get following error:
Index in position 2 exceeds array bounds (must not exceed 1).
Thanks
Here is the code:
clc;
clear all;
close all;
W_C=[0.35 0.5 0.65 0.80];
L_C=[3 4.5 6 7.5];
H=0.035;
epsilon_r=3.38;
for i=1:length(W_C)
W_C=W_C(1,i);
epsilon_o=8.85418782*10^(-12);
epsilon_re=0.5*(epsilon_r+1)+0.5*(epsilon_r-1)*(1+12*(H./W_C)).^(-0.5);
C_I=0.5*epsilon_o*(epsilon_r+1)*(5.5.*L_C);
L_series=50*(1/(3*10^(8)))*sqrt(epsilon_re).*L_C;
R_s=1.5*10^(-6)*sqrt(2.6*10^9);
R_series=(4/3).*(L_C./(6.*W_C))*R_s;
end
disp(C_I)
disp(L_series)
disp(R_series)

답변 (1개)

Image Analyst
Image Analyst 2022년 8월 21일
Is this what you want?
clc;
clear all;
close all;
W_C=[0.35 0.5 0.65 0.80];
L_C=[3 4.5 6 7.5];
H=0.035;
epsilon_r=3.38;
for i=1:length(W_C)
W=W_C(1,i);
for k=1:length(L_C)
L = L_C(k);
epsilon_o=8.85418782*10^(-12);
epsilon_re=0.5*(epsilon_r+1)+0.5*(epsilon_r-1)*(1+12*(H./W)).^(-0.5);
C_I=0.5*epsilon_o*(epsilon_r+1)*(5.5.*L);
L_series=50*(1/(3*10^(8)))*sqrt(epsilon_re).*L;
R_s=1.5*10^(-6)*sqrt(2.6*10^9);
R_series=(4/3).*(L./(6.*W))*R_s;
fprintf('W = %.2f, L = %.2f, C_I = %.6f, L_series = %.6f, R_series = %.6f\n', ...
W, L, C_I, L_series, R_series)
end
end
W = 0.35, L = 3.00, C_I = 0.000000, L_series = 0.000001, R_series = 0.145686 W = 0.35, L = 4.50, C_I = 0.000000, L_series = 0.000001, R_series = 0.218529 W = 0.35, L = 6.00, C_I = 0.000000, L_series = 0.000002, R_series = 0.291373 W = 0.35, L = 7.50, C_I = 0.000000, L_series = 0.000002, R_series = 0.364216 W = 0.50, L = 3.00, C_I = 0.000000, L_series = 0.000001, R_series = 0.101980 W = 0.50, L = 4.50, C_I = 0.000000, L_series = 0.000001, R_series = 0.152971 W = 0.50, L = 6.00, C_I = 0.000000, L_series = 0.000002, R_series = 0.203961 W = 0.50, L = 7.50, C_I = 0.000000, L_series = 0.000002, R_series = 0.254951 W = 0.65, L = 3.00, C_I = 0.000000, L_series = 0.000001, R_series = 0.078446 W = 0.65, L = 4.50, C_I = 0.000000, L_series = 0.000001, R_series = 0.117670 W = 0.65, L = 6.00, C_I = 0.000000, L_series = 0.000002, R_series = 0.156893 W = 0.65, L = 7.50, C_I = 0.000000, L_series = 0.000002, R_series = 0.196116 W = 0.80, L = 3.00, C_I = 0.000000, L_series = 0.000001, R_series = 0.063738 W = 0.80, L = 4.50, C_I = 0.000000, L_series = 0.000001, R_series = 0.095607 W = 0.80, L = 6.00, C_I = 0.000000, L_series = 0.000002, R_series = 0.127475 W = 0.80, L = 7.50, C_I = 0.000000, L_series = 0.000002, R_series = 0.159344

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by