How can I avoid looping here?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Behrooz Daneshian
il 20 Gen 2023
Modificato: Alberto Cuadra Lara
il 20 Gen 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 Commenti
Risposta accettata
Torsten
il 20 Gen 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 Commenti
Più risposte (1)
Alberto Cuadra Lara
il 20 Gen 2023
Modificato: Alberto Cuadra Lara
il 20 Gen 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 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!