find if two numbers are BOTH present in an array

7 ビュー (過去 30 日間)
suleiman abdullahi
suleiman abdullahi 2019 年 6 月 30 日
コメント済み: Steven Lord 2019 年 6 月 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 件のコメント
suleiman abdullahi
suleiman abdullahi 2019 年 6 月 30 日
thank for the help, i have rephrased the quesition.
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by