using a while loop statement, individually display the apples element in the "fruits" array.
1 回表示 (過去 30 日間)
古いコメントを表示
fruits = ["apricot", "pears", "pineapples", "apples"];
1 件のコメント
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))
回答 (1 件)
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" ?
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!