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!