필터 지우기
필터 지우기

How do I make a condition statement for an element that presents in two different arrays but is at the wrong position? This also applies to more than one elements

조회 수: 2 (최근 30일)
final_code_make = [5 2 3 1]
final_code_guess = [ 1 3 2 4]
As it appears, there are few same elements in both arrays but the arrangement is wrong
  댓글 수: 2
James Tursa
James Tursa 2021년 11월 17일
편집: James Tursa 2021년 11월 17일
It is not clear what you want. Do you want to find out if there are elements common to both arrays? Do you want to find the entire subset of common elements? Do you want to find the positions in both arrays of the common elements? Or ...? What would be the desired output of your example?
S Suhaimi
S Suhaimi 2021년 11월 17일
code_guess1 = input ('first position: ');
code_guess2 = input ('second position: ');
code_guess3 = input ('third position: ');
code_guess4 = input ('fourth position: ');
final_code_guess = [code_guess1 code_guess2 code_guess3 code_guess4]
for i = 1:4
if final_code_guess(i) == final_code_make(i)
fprintf ('Your number %i guess exists and is in the correct position\n\n', i )
elseif final_code_guess(i)~=final_code_make(i)
fprintf ('your number %i guess is either does not exist or in the wrong position\n\n', i )
end
% I want to add another elseif statement that states the element does exist but is in the wrong position

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2021년 11월 17일
Assuming you have already made the check for the correct number in the correct spot, you can use this
ismember(final_code_guess(i), final_code_make)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by