How can I avoid looping here?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Torsten
2023-1-20
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 个评论
更多回答(1 个)
Alberto Cuadra Lara
2023-1-20
编辑:Alberto Cuadra Lara
2023-1-20
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!