How to eliminate the for loop but end with the same result
1 回表示 (過去 30 日間)
古いコメントを表示
A = input('Using brackets, enter a vector: ')
for i=1:length(A)
if(A(i) > 0)
B(i) = A(i).^3;
else
B(i) = 0;
end
end
B
How can I convert this code so that when the for loop and if statement are deleted the code still has the same output?
0 件のコメント
採用された回答
Star Strider
2022 年 9 月 2 日
Try something like this —
A = -3:3;
B = A.^3.*(A>0)
It uses a version of ‘logical indexing’.
.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!