Azzera filtri
Azzera filtri

How to eliminate the for loop but end with the same result

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Star Strider
Star Strider il 2 Set 2022
Try something like this —
A = -3:3;
B = A.^3.*(A>0)
B = 1×7
0 0 0 0 1 8 27
It uses a version of ‘logical indexing’.
.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by