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

1 次查看(过去 30 天)
fruits = ["apricot", "pears", "pineapples", "apples"];
  1 个评论
DGM
DGM 2023-8-30
编辑:DGM 2023-8-30
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

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2023-8-29
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" ?

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by