How do I use the for loop to replace elements in an array?

8 ビュー (過去 30 日間)
Valentin Neme Gabriel
Valentin Neme Gabriel 2022 年 4 月 12 日
回答済み: Voss 2022 年 4 月 12 日
My array doesn't have any requirements.

回答 (1 件)

Voss
Voss 2022 年 4 月 12 日
array = randi(7,1,20);
disp(array);
4 3 3 5 7 7 7 6 1 5 7 4 1 6 5 1 2 4 2 7
% use a for loop to replace all the 4's in array with 19's:
for ii = 1:numel(array)
if array(ii) == 4
array(ii) = 19;
end
end
disp(array);
19 3 3 5 7 7 7 6 1 5 7 19 1 6 5 1 2 19 2 7

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by