How convert for loop to while loop?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hasan Berke Bankoglu
le 23 Mai 2020
Commenté : Hasan Berke Bankoglu
le 23 Mai 2020
I must convert that for loop to while loop. How can I do that? I tried somethings but I couldn't get right result.
A=zeros(5,8);
[r,c]=size(A);
%for loop
for i=1:r % loops over rows
for j=1:c % loops over columns
if i>j
A(i,j)=4*i-2*j;
elseif i<=j
A(i,j)=i^2-3*j;
end
end
end
A
0 commentaires
Réponse acceptée
KSSV
le 23 Mai 2020
A=zeros(5,8);
[r,c]=size(A);
%for loop
i = 0 ;
while i<r % loops over rows
i = i+1 ;
j = 0 ;
while j<c % loops over columns
j = j+1 ;
if i>j
A(i,j)=4*i-2*j;
elseif i<=j
A(i,j)=i^2-3*j;
end
end
end
A
3 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!