assigning answers of logical operators to a vector using a for-loop
조회 수: 1 (최근 30일)
이전 댓글 표시
I was asked to write a function called testLucky.m which will text if inputed values are in the "secretLucky" matrix using a for-loop. The desired output is a logical 1 or 0 representing whether the inputted value is in the matrix or not. Here's what I've got so far:
My problem is that i onlhy want one output - 0 or 1. but I can't figure out how to do it without getting 7 values enery time (for each for statement)
function[y] = testLucky(x)
% testLucky(x) - determines whether input(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];
for k=1:7
secretLucky(i)==x
end
end
댓글 수: 0
답변 (1개)
KSSV
2022년 3월 7일
편집: KSSV
2022년 3월 7일
function y = testLucky(x)
% testLucky(x) - determines whether input(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];
y = false ;
for k=1:7
if secretLucky(k)==x
y = true ;
break
end
end
end
You can also use ismember. Read about this.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!