Filter löschen
Filter löschen

How to make a function that outputs true or false (logical) if a number inputted is in a predetermined vector?

77 Ansichten (letzte 30 Tage)
% Here is my code:
function [x] = testLucky1
% testLucky(x) - determine whether x is a lucky number
% return true (1) if x is a lucky number
% return false (0) if x is not a lucky number
secretLucky = [19,22,5,9,11,15,21];
x = input('Enter a number: ');
for k = 1:length(secretLucky)
if secretLucky(k) == x
true
else
false
end
end
% The issue I'm having is that when I enter a single number like
% 19/22/5/9/11/15/21, the command window shows a logical statement of 1
% alongside logical statements of 0.
% For example, if I input 19, the first logical statement in the command
% window will be 1, and the remaining 6 logical statements will be 0.
% My question is, how can I make the output in the command window be a
% SINGLE logical statement of 1 if ANY of the numbers in vector secretLucky
% is entered? And if a number that is not ANY of the numbers in vector
% secretLucky is entered, how can I have a SINGLE logical statement of 0?
% Does my issue stem from the fact the ouput of true/false is based on my
% variable k? If so what should I do to change this?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 1 Mär. 2022
found = false;
for k = 1:10
if x(k) == 7
found = true;
end
end
found
  2 Kommentare
Jacob Boulrice
Jacob Boulrice am 1 Mär. 2022
Hey, thank you for the response.
Any chance you could explain what this means? And where I should add this in my code?
One question I have is why you set k to 1:10?
Walter Roberson
Walter Roberson am 2 Mär. 2022
The intent is to illustrate initializing a state variable, changing it conditionally, and then examining the state afterwards.
Key point: do not display true when you find a match, set the state variable instead. And do not display the false when a match fails, just go on to the next test. You do not care in this context whether the input did not match 7 out of the 8 possibilities: you only care that it matched one of the possibilities.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Davide Masiello
Davide Masiello am 1 Mär. 2022
Bearbeitet: Davide Masiello am 1 Mär. 2022
function testLucky
% testLucky - determine whether x is a lucky number
% return true (1) if x is a lucky number
% return false (0) if x is not a lucky number
secretLucky = [19,22,5,9,11,15,21];
x = input('Enter a number: ');
result = false;
for i = 1:numel(secretLucky)
if x == secretLucky(i)
result = true;
end
end
result
end
  2 Kommentare
Jacob Boulrice
Jacob Boulrice am 1 Mär. 2022
Unfortentatly I need to use a for loop. We just learned about them in class so this homework is meant to teach me how to use them.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by