using a while loop statement, individually display the apples element in the "fruits" array.
1 view (last 30 days)
Show older comments
fruits = ["apricot", "pears", "pineapples", "apples"];
1 Comment
DGM
on 30 Aug 2023
Edited: DGM
on 30 Aug 2023
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))
Answers (1)
Walter Roberson
on 29 Aug 2023
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" ?
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!