How to eliminate the for loop but end with the same result
조회 수: 2 (최근 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!