Undefined function 'count' for input arguments of type 'logical'.

조회 수: 3 (최근 30일)
yiwen yim
yiwen yim 2018년 3월 17일
댓글: Image Analyst 2018년 3월 18일
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
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
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
yiwen yim
yiwen yim 2018년 3월 18일
This is the dataset file. please advice.
Image Analyst
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 CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by