Algorithm for equality condition.

1 次查看(过去 30 天)
Abhinav
Abhinav 2015-2-25
b5=zeros(5,1)
b6=zeros(5,1)
b1=[ 1417.1;
1299;
236.19;
501.9;
708.57]
while (~((b5+b6)==b1))
restart = true;
while restart
restart = false; %in case it succeeds
for i = 1:5
s5(i, :) = randi([0 1],1,6);
xl5=0;
xu5=4000;
l5=6;
si5=bi2de(s5);
b5=(xl5+((xu5-xl5)/((2^l5)-1)))*si5;
if all(s5(i, :) == 0)
restart = true;%stop the for loop, thus restarting from scratch
end
end
end
end
disp([s5,si5,b5]);
restart = true;
while restart
restart = false; %in case it succeeds
for i = 1:5
s6(i, :) = randi([0 1],1,6);
xl6=0;
xu6=4000;
l6=6;
si6=bi2de(s6);
b6=(xl6+((xu6-xl6)/((2^l6)-1)))*si6;
if all(s6(i, :) == 0)
restart = true;%stop the for loop, thus restarting from scratch
end
end
end
disp([s6,si6,b6]);
b5+b6
Is there any fast method to satisfy the first constraint (~((b5+b6)==b1)) I am doing PSO optimization and this constraint has to be satisfied. Normally it is going on and going on and not MATLAB status is Busy
  2 个评论
John D'Errico
John D'Errico 2015-2-25
编辑:John D'Errico 2015-2-25
Please use the code button "{} Code" when you post code. it is unreadable otherwise. I've done it for you this time.
John D'Errico
John D'Errico 2015-2-25
编辑:John D'Errico 2015-2-25
I looked at your code once I could read it. A bunch of numbered variables, that mean absolutely nothing to anyone but you. I love this one, by the way:
l5=6;
Yes, it probably means l5, where l is a lower case L. But if your goal is to create unreadable code, you should recognize that this variable is virtually undistinguishable with the number 15. Depends on the font it is viewed with of course.
No hint as to what the code is supposed to do, so when you ask for a fast way to do it, we have no real idea what is the goal, so finding a faster way to do what we don't know what you are doing is impossible.
Your comments don't even make sense. For example, this one:
restart = true;%stop the for loop, thus restarting from scratch
restart has nothing to do with stopping any for loop it is contained in. Once that for loop does terminate, restart allows the while loop around it all to continue working.
Confusing spaghetti code, with no clue provided as to what it does. How can we possibly help you? My point is, if you want help, then help those who could possibly help you. If you can't write clear code, at least explain what it does, or what it should do!

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2015-2-25
Use a tolerance to compare floating point numbers for equality. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F and tutorial on formatting questions

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by