find if two numbers are BOTH present in an array
9 views (last 30 days)
Show older comments
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 Comment
Steven Lord
on 30 Jun 2019
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.
Answers (1)
KALYAN ACHARJYA
on 30 Jun 2019
Edited: KALYAN ACHARJYA
on 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 Comments
KALYAN ACHARJYA
on 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.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!