find if two numbers are BOTH present in an array
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Steven Lord
el 30 de Jun. de 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.
Respuestas (1)
KALYAN ACHARJYA
el 30 de Jun. de 2019
Editada: KALYAN ACHARJYA
el 30 de Jun. de 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 comentarios
KALYAN ACHARJYA
el 30 de Jun. de 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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!