필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Loop "if arg in ..."

조회 수: 1 (최근 30일)
Lucas S
Lucas S 2019년 4월 5일
마감: MATLAB Answer Bot 2021년 8월 20일
Hello,
I come from Python and i would like to know if there is a
if a in b : ...
end
loop in matlab.
Thanks !

답변 (1개)

Jonathan A
Jonathan A 2019년 4월 12일
Hi Lucas,
Not sure what you are referring to when you say that the "if" statement is a loop. But, if my assumption is correct that you are trying to check whether or not a value or an object is present in a list, you could have multiple options depending on the data in your list (matrix format, cell array format, uniform element class, ...), see code below:
% Case 1: b is a matrix (as strings can be considered as character vectors in Matlab,
% this works also for b='iop' and a='p')
b = [1,2,3];
a = 2;
if any(b==a)
disp('Yes');
else
disp('No');
end
% Case 2: b is a cell array of character vectors
b = {'q','r','s'};
a = 's';
if any(ismember(b,a))
disp('Yes');
else
disp('No');
end
% Case 3: b is a cell array of anything
b = {'q',1,[1,2]};
a = [1,2];
if any(cellfun(@(x) isequal(a,x),b))
disp('Yes');
else
disp('No');
end
I hope this helps.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by