How can I avoid looping here?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Behrooz Daneshian
am 20 Jan. 2023
Bearbeitet: Alberto Cuadra Lara
am 20 Jan. 2023
Hi all,
I want to avoid using for loop in part of my code to make it more efficient. Can anyone give me idea regarding this?
STD_w=0.05;
STD_gama=5;
mean_w=linspace(15,40,10);
sigma_w=ones(1,length(mean_w))*STD_w;
mean_gama=linspace(115,85,10);
sigma_gama=ones(1,length(mean_w))*STD_gama;
mean_k=zeros(1,length(mean_w));
mean_L=zeros(1,length(mean_w));
for j=1:length(mean_w)
mean_k(j)=(0.0833/2)*(0.01*10^(0.022*mean_gama(j))+0.085*10^(0.008*mean_gama(j))*mean_w(j)+(0.9*log10(mean_w(j))-0.2)*10^(0.01*mean_gama(j)));
mean_L(j)=144*0.01*mean_w(j)*mean_gama(j);
end
0 Kommentare
Akzeptierte Antwort
Torsten
am 20 Jan. 2023
STD_w=0.05;
STD_gama=5;
mean_w=linspace(15,40,10);
sigma_w=ones(1,length(mean_w))*STD_w;
mean_gama=linspace(115,85,10);
sigma_gama=ones(1,length(mean_w))*STD_gama;
mean_k=(0.0833/2)*(0.01*10.^(0.022*mean_gama)+0.085*10.^(0.008*mean_gama).*mean_w+(0.9*log10(mean_w)-0.2).*10.^(0.01*mean_gama))
mean_L=144*0.01*mean_w.*mean_gama
0 Kommentare
Weitere Antworten (1)
Alberto Cuadra Lara
am 20 Jan. 2023
Bearbeitet: Alberto Cuadra Lara
am 20 Jan. 2023
Hello Behrooz,
By using elementwise operators {.* .^ ./}.
Best,
Alberto
% Definitions
STD_w=0.05;
STD_gama=5;
mean_w=linspace(15,40,10);
sigma_w=ones(1,length(mean_w))*STD_w;
mean_gama=linspace(115,85,10);
sigma_gama=ones(1,length(mean_w))*STD_gama;
mean_k=zeros(1,length(mean_w));
mean_L=zeros(1,length(mean_w));
% With loop
for j=1:length(mean_w)
mean_k(j)=(0.0833/2)*(0.01*10^(0.022*mean_gama(j))+0.085*10^(0.008*mean_gama(j))*mean_w(j)+(0.9*log10(mean_w(j))-0.2)*10^(0.01*mean_gama(j)));
mean_L(j)=144*0.01*mean_w(j)*mean_gama(j);
end
% Without loop
mean_k_2 = (0.0833/2)*(0.01*10.^(0.022*mean_gama) + 0.085*10.^(0.008*mean_gama) .* mean_w + (0.9*log10(mean_w)-0.2) .* 10.^(0.01*mean_gama));
mean_L_2 = 144 * 0.01 * mean_w .* mean_gama;
% Checks
FLAG_CHECKS = isequal(mean_k, mean_k_2) && isequal(mean_L, mean_L_2)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!