using a while loop statement, individually display the apples element in the "fruits" array.

fruits = ["apricot", "pears", "pineapples", "apples"];

1 comentario

If we take "the fruits array" to mean the specific example given, then there is only one valid candidate. Given that, it's not clear that a loop is needed, let alone a while loop.
If we're talking about any list of fruit names, then like Walter said, it's important to be specific about what you want when you say "apples elements". It's also still unclear why a loop is needed.
% a list of fruit names
fruits = ["apricot", "pears", "pineapples", "apples", "Apple"];
% true for "apple" or "apples" (case-insensitive)
isapples = ismember(lower(fruits),{'apple','apples'});
% display the elements of the fruits array which meet the simple test
fprintf('%s\n',fruits(isapples))
apples Apple

Iniciar sesión para comentar.

Respuestas (1)

N2014671 = 0;
while N2014671 < numel(fruits)
N2014671 = N2014671 + 1;
T2014671 = fruits(N2014671);
if F2014671(T2014671)
fprintf('At index %d there was: ', N2014671); disp(T2014671);
end
end
function R2014671 = F2014671(V2014671)
R2014671 = false;
if V2014671 is an example of what you are searching for
R2014671 = true;
end
end
.. You need to define how to know whether a value is an example of what you are searching for. For example, if "apples" is acceptable, how about "Apples" or "apple", or "crabapple", or "McIntosh" or "mcintosh" or "pineapple" or "pomme" ?

Categorías

Etiquetas

Preguntada:

el 29 de Ag. de 2023

Editada:

DGM
el 30 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by