Clear Filters
Clear Filters

Avoid "for", "if" and "else" loop

4 views (last 30 days)
Hokkien
Hokkien on 29 Sep 2021
Commented: Matt J on 29 Sep 2021
Hi I have a code like this:
If there any way, that I can avoid for, if else loop? For now the k = 22, but most of the time and i have k closed to 9000+ and it took a lot of computation time. Thanks!
for k=1:22
for j=1:1400
if Ev(k,j)==0 | (((E_beta(k,1)-Ev(k,j))^2)-(0.511^2))<0, p(k,j)=0;
else
p(k,j)=sqrt(((E_beta(k,1)-Ev(k,j))^2)-(0.511^2));
end
end
end

Accepted Answer

Matt J
Matt J on 29 Sep 2021
Edited: Matt J on 29 Sep 2021
D=(E_beta-Ev).^2-0.511^2;
D( D<0 | ~Ev)=0;
p=sqrt(D);
  2 Comments
Hokkien
Hokkien on 29 Sep 2021
Hi Matt tahnk you! By the way, what's the meaning for ~Ev?
Matt J
Matt J on 29 Sep 2021
You're welcome, but pleae Accept-click the answer if it does what you need.
~Ev is the same thing as Ev==0.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!