find if two numbers are BOTH present in an array

7 次查看(过去 30 天)
Hello everybody,
Given 2 numbers p and q and a vector v, the question is, are BOTH elements in v?
by defining a function with both numbers p,q and vector v as input and the answer to above question being (either yes or no), the only allowed matlab routine is 'length' and only one loop can be used. the function needs to be tested.
this is what i had;
function [logica] = find(p,q,v)
for k = 1:length(v)
if v(k)==p || v(k)==q
logic = yes
else
logic = No
end
end
but this will only show either p or q is present and not both of them are present
Thanks.
  1 个评论
Steven Lord
Steven Lord 2019-6-30
You should not call your function find. That already has a meaning in MATLAB.
You said "the only allowed matlab routine is 'length' " but that's not true. You're using ==, ||, and probably true and false in addition to length. Since this sounds like a homework assignment, you probably want to clarify with your professor or teaching assistant specifically what's not allowed. I'm guessing they didn't want you to simply call ismember.

请先登录,再进行评论。

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-6-30
编辑:KALYAN ACHARJYA 2019-6-30
Original question:
A=[2 3 7 9 8; 9 2 4 1 0];
find if two numbers are BOTH present in an array, let 3 and 9 . the result like yes=1.
y=find(A==3) & find(A==9);
if y==1
disp('Yes');
else
disp('No')
end
  2 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2019-6-30
In previous question, I answerd what you have asked for.
It would be better, if you want any extention (related only) of the question, you can put it in comment section.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by