Please help me to rewrite the following codes
5 views (last 30 days)
Show older comments
%% Calculation of phi and f
a11 = ai(1);
a12 = ((ai(1)*ai(2))^0.5)*(1-bin_PR(1,2));
a13 = ((ai(1)*ai(3))^0.5).*(1-bin_PR(1,3));
a21 = a12;
a22 = ai(2);
a23 = ((ai(2)*ai(3))^0.5)*(1-bin_PR(2,3));
a31 = a13;
a32 = a23;
a33 = ai(3);
sum_xa(1) = x(1)*a11 + x(2)*a12 +x(3)*a13;
sum_xa(2) = x(1)*a21 + x(2)*a22 +x(3)*a23;
sum_xa(3) = x(1)*a31 + x(2)*a32 +x(3)*a33;
sum_ya(1) = y(1)*a11 + y(2)*a12 +y(3)*a13;
sum_ya(2) = y(1)*a21 + y(2)*a22 +y(3)*a23;
sum_ya(3) = y(1)*a31 + y(2)*a32 +y(3)*a33;
phi_L=exp((bi./b_L).*(ZLt-1)-log(ZLt-B_L)-(A_L./(B_L.*(-2*sqrt(2)))).*(log((ZLt+(1-sqrt(2)).*B_L)/(ZLt+(1+sqrt(2)).*B_L)))*((2*sum_xa/a_L)-(bi./b_L)));
phi_G=exp((bi./b_G).*(ZGt-1)-log(ZGt-B_G)-(A_G./(B_G.*(-2*sqrt(2)))).*(log((ZGt+(1-sqrt(2)).*B_G)/(ZGt+(1+sqrt(2)).*B_G)))*((2*sum_ya/a_G)-(bi./b_G)));
------------------------------------
-----------------------------------------
I want to write the above coding like this (below one):
for i=1:3
for j=1:3
el_L(i,j)=x(i).*x(j)*((ai(i).*ai(j)).^0.5).*(1-bin_PR(i,j));
el_G(i,j)=y(i).*y(j)*((ai(i).*ai(j)).^0.5).*(1-bin_PR(i,j));
end
end
a_L=sum(el_L(:));
b_L=sum(x.*bi);
7 Comments
madhan ravi
on 19 Nov 2018
no you won't if you chane three to twenty you will get the results if and only if the other variables have the compatable sizes
Accepted Answer
KSSV
on 19 Nov 2018
YOu need not to use a loop for your code. YOu can achieve it by simple matrix multiplication. Check the below code.
a11 = ai(1);
a12 = ((ai(1)*ai(2))^0.5)*(1-bin_PR(1,2));
a13 = ((ai(1)*ai(3))^0.5).*(1-bin_PR(1,3));
a21 = a12;
a22 = ai(2);
a23 = ((ai(2)*ai(3))^0.5)*(1-bin_PR(2,3));
a31 = a13;
a32 = a23;
a33 = ai(3);
A = [a11 a12 a13 ; a21 a22 a23;a31 a32 a33] ;
b1 = x ; % this should be a 3X1 array
b2 = y ; % this should be a 3X1 array
sum_xa = A*b1 ;
sum_ya = A*b2 ;
phi_L=exp((bi./b_L).*(ZLt-1)-log(ZLt-B_L)-(A_L./(B_L.*(-2*sqrt(2)))).*(log((ZLt+(1-sqrt(2)).*B_L)/(ZLt+(1+sqrt(2)).*B_L)))*((2*sum_xa/a_L)-(bi./b_L)));
phi_G=exp((bi./b_G).*(ZGt-1)-log(ZGt-B_G)-(A_G./(B_G.*(-2*sqrt(2)))).*(log((ZGt+(1-sqrt(2)).*B_G)/(ZGt+(1+sqrt(2)).*B_G)))*((2*sum_ya/a_G)-(bi./b_G)));
2 Comments
KSSV
on 19 Nov 2018
a11 = ai(1);
a12 = ((ai(1)*ai(2))^0.5)*(1-bin_PR(1,2));
a13 = ((ai(1)*ai(3))^0.5).*(1-bin_PR(1,3));
a21 = a12;
a22 = ai(2);
a23 = ((ai(2)*ai(3))^0.5)*(1-bin_PR(2,3));
a31 = a13;
a32 = a23;
a33 = ai(3);
A = [a11 a12 a13 ; a21 a22 a23;a31 a32 a33] ;
sum_xa = zeros(3,1) ;
sum_ya = zeros(3,1) ;
for i = 1:3
sum_xa(i) = A(i,1)*x(1)+A(i,2)*x(2)+A(i,3)*x(3) ;
sum_ya(i) = A(i,1)*y(1)+A(i,2)*y(2)+A(i,3)*y(3) ;
end
phi_L=exp((bi./b_L).*(ZLt-1)-log(ZLt-B_L)-(A_L./(B_L.*(-2*sqrt(2)))).*(log((ZLt+(1-sqrt(2)).*B_L)/(ZLt+(1+sqrt(2)).*B_L)))*((2*sum_xa/a_L)-(bi./b_L)));
phi_G=exp((bi./b_G).*(ZGt-1)-log(ZGt-B_G)-(A_G./(B_G.*(-2*sqrt(2)))).*(log((ZGt+(1-sqrt(2)).*B_G)/(
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!