Undefined function 'count' for input arguments of type 'logical'.
1 次查看(过去 30 天)
显示 更早的评论
Im trying to code my fitness function with just one constraint inside that m.file but i run into and error that says
"Undefined function 'count' for input arguments of type 'logical'.
Error in my_fun (line 22)
if count (L == 1) == 1; "
I don't really how to fix it. the following is the piece of code.
function y = my_fun(c1)
w1 = 0.8;
% w2 = 0.8;
% w3 = 0.8;
% y = w1*x1 + w2*x2 + w3*x3;
X=xlsread('Dataset.xlsx');
% Xout=X(:,end);
D = 1:5;
T = 1:9;
L = 1:26;
chromosome = [D T L];
%Constraint 1
for DateID = D
for TimeID = T
if count (L == 1) == 1;
c1 = 0;
else
c1 = 1;
end
end
end
y = w1*c1;
In here, im trying to define my chromosome in the form of [D T L] and also assigning the ID in my dataset to be the genotype. In constraint 1, im trying to find if the L have violated the constraints and it gives the value 1, and if not then 0.
PLease advice. thanks.
1 个评论
dpb
2018-3-17
编辑:dpb
2018-3-17
I can't follow what you're trying to do here, sorry.
The error is that count is a builtin string function that returns the number instances of a given STRING pattern within another string and you've defined D, T, L all as integers, not character plus you have only a single argument into count that is a logical expression, not a string and rather than two character arguments needed of the string and pattern string to match/count.
Also since L=1:26 and is never modified, nothing is going to happen inside the loops that's any different than without any loop; there's nothing referenced inside the loop that isn't invariant for the loop indices.
NB
L==1 will return a logical vector of T/F but since L is fixed it will be T for the first position and F everywhere else. Also look up how IF works in Matlab; the condition will be T iff all elements in the quantity are T; hence that test even if it were coded correctly would never be T and the IF branch never executed.
Show us a small test case of inputs and expected outputs to help with the verbal explanation...
采纳的回答
Image Analyst
2018-3-17
Perhaps you want sum() to count the number of 1's in the array? Like this?
if sum(L == 1) == 1
By the way, no semicolon is needed at the end of an if statement.
Please attach 'Dataset.xlsx' if you need more help.
By the way, why do you pass in c1 to the function when you just ignore it by overwriting it?
2 个评论
Image Analyst
2018-3-18
Please explain how X is used in your loop. You are not even looking at it inside your loop for constraint 1. You are just looking at L, which is a vector of constants not having anything to do with your data X whatsoever.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!