Filter löschen
Filter löschen

find if two numbers are BOTH present in an array

6 Ansichten (letzte 30 Tage)
suleiman abdullahi
suleiman abdullahi am 30 Jun. 2019
Kommentiert: Steven Lord am 30 Jun. 2019
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 Kommentar
Steven Lord
Steven Lord am 30 Jun. 2019
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 30 Jun. 2019
Bearbeitet: KALYAN ACHARJYA am 30 Jun. 2019
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 Kommentare
suleiman abdullahi
suleiman abdullahi am 30 Jun. 2019
thank for the help, i have rephrased the quesition.
KALYAN ACHARJYA
KALYAN ACHARJYA am 30 Jun. 2019
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by