My code taking too much time

2 次查看(过去 30 天)
Here is my code. How can I reduce that time without changing the result?
clear all;clc;close all;
L=1000;
c = randn(1,1000000);
cont3 = 1;
while cont3 < length(c)+1
if(abs(c(cont3)) < 1 || abs(c(cont3)) > 10)
c(cont3) = randn(1);
cont3 = 0;
end
cont3 = cont3+1;
end
c;

采纳的回答

Jon
Jon 2020-11-16
编辑:Jon 2020-11-16
You can operate on the entire vector using for example
a(a<1) = randn
So you could make a loop something like
while any(a<1) || any(a>10)
a(a<1) = randn(sum(a<1),1)
a(a>10) = randn(sum(a>10),1)
end
  5 个评论
Jon
Jon 2020-11-16
Sorry I didn't look at your specification carefully. Anyhow you should be able to modify
while any(abs(a)<1) || any(abs(a)>10)
condition1 = abs(a) < 1;
a(condition1) = randn(sum(condition1),1);
condition2 = abs(a) > 10;
a(condition2) = randn(sum(condition2),1);
end
Mr.Chandler
Mr.Chandler 2020-11-16
Thanks a lot. This solved my problem exactly.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by