Error shows Not enough input arguments

3 Ansichten (letzte 30 Tage)
Ruoming Xu
Ruoming Xu am 7 Aug. 2021
Kommentiert: Image Analyst am 8 Aug. 2021
Hello, I got one question and it keeps showing Not enought input arguments as it's error code, and cannot get a proper answer from it.
The questions is to create a logic array with true for any location where the runner is on the Red Team (R) with a running time less than 10.
The Function I wrote is :
function qualifyingIndex=FindQualify(rTeams, rTimes)
FindQualifyLocs=[(rTeams=='G'),(rTimes<10)];
qualifyingIndex=FindQualify.*FindQualifyLocs;
end
and the code to call my function is :
FindQualify(['R','B','R'],[10.1,8,11])

Akzeptierte Antwort

Image Analyst
Image Analyst am 7 Aug. 2021
Try this:
qualifyingIndex = (rTeams == 'R') & (rTimes < 10);
Full Demo:
rTeams = ['R','B','R'];
rTimes = [10.1,8,11];
logicalVector = FindQualify(rTeams, rTimes)
if all(~logicalVector)
fprintf('No member of team R had a time less than 10.\n');
else
% List them all
for k = find(logicalVector)
fprintf('Racer #%d on team R had a time of %.1f.\n', k, rTimes(k));
end
end
rTeams = ['R','B','R'];
rTimes = [7.1,8,9.4];
logicalVector = FindQualify(rTeams, rTimes)
if all(~logicalVector)
fprintf('No member of team R had a time less than 10.\n');
else
% List them all
for k = find(logicalVector)
fprintf('Racer #%d on team R had a time of %.1f.\n', k, rTimes(k));
end
end
function qualifyingIndex = FindQualify(rTeams, rTimes)
% Get a logical vector where the team = R and the time is less than 10.
qualifyingIndex = (rTeams == 'R') & (rTimes < 10);
end
For the two cases, you'll see:
logicalVector =
1×3 logical array
0 0 0
No member of team R had a time less than 10.
logicalVector =
1×3 logical array
1 0 1
Racer #1 on team R had a time of 7.1.
Racer #3 on team R had a time of 9.4.
  2 Kommentare
Ruoming Xu
Ruoming Xu am 7 Aug. 2021
Thanks! I have already solve this question! The code I'm using:
function qualifyingIndex=FindQualify(rTeams, rTimes)
FindQualifyLocs=(rTeams=='R')&(rTimes<10);
qualifyingIndex=1.*FindQualifyLocs;
end
Image Analyst
Image Analyst am 8 Aug. 2021
Yeah, that's totally unnecessary to make that temporary variable, FindQualifyLocs.
FindQualifyLocs is already a logical variable so you're done at that point. The subsequent line where you multiply it by one actually turns it into a double, not a logical like you said you wanted. So that's wrong. Remember you said you wanted "to create a logic array" -- well you created a double, not a logical.
The correct way would be like I showed - in a single line. So it would be
function qualifyingIndex = FindQualify(rTeams, rTimes)
% Get a logical vector where the team = R and the time is less than 10.
qualifyingIndex = (rTeams == 'R') & (rTimes < 10);
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 7 Aug. 2021
qualifyingIndex=FindQualify.*FindQualifyLocs;
Notice in that line you have FindQualify, which is the name of the function that you are defining. That is a request to invoke that same function recursively, but this time passing in no arguments.
Unfortunately there is no documentation as to what the function is intended to do, so I cannot suggest a repair.
  2 Kommentare
Ruoming Xu
Ruoming Xu am 7 Aug. 2021
Hi, thanks for your reply! I might typed wrong about the function, the rTeams should be rTeams=='B', but not 'G'. The function suppose to give a return like [0,1,0], since for the second one, it's both rTeams=='B', and rTimes<10.
Walter Roberson
Walter Roberson am 7 Aug. 2021
[(rTeams=='B'),(rTimes<10)]
rTeams is a vector of length 3. rTimes is a vector of length 3. When you put the two vectors together with [] you get a vector of length 6.
You then try to multiply that vector of length 6 by... something. You are expecting a vector of length 3 as the result.
In order for multiplication of 1 x 6 vector to give a 1 x 3 result, you would need to be using
A is some 3 x 6 array
result = (A * FindQualifyLocs.').'
where A is some 3 x 6 array. Using * between a 3 x 6 and a (1 x 6 transposed to be 6 x 1) would give 3 x 1, and you would transpose that to get a 1 x 3.
But what should that array A be? I do not know.
I would suggest to you that [(rTeams=='B'),(rTimes<10)] is the wrong thing to compute. https://www.mathworks.com/help/matlab/ref/and.html

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by