i want to use while loop

2 vues (au cours des 30 derniers jours)
diadalina
diadalina le 6 Jan 2023
Commenté : diadalina le 7 Jan 2023
can anyone help to convert this code to the while loop :
n=5;
for i=1:n
for j=1:n
if i+j-1>n
H(i,j)=0;
else
H(i,j)=i+j-1;
end
end
end
H
H = 5×5
1 2 3 4 5 2 3 4 5 0 3 4 5 0 0 4 5 0 0 0 5 0 0 0 0

Réponse acceptée

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 6 Jan 2023
Here is the version of your code with the [while... end] operation:
n=5;
i=1;
while i<=n
j=1;
while j<=n
if i+j-1>n
H(i,j)=0;
else
H(i,j)=i+j-1;
end
j=j+1;
end
i=i+1;
end
H
H = 5×5
1 2 3 4 5 2 3 4 5 0 3 4 5 0 0 4 5 0 0 0 5 0 0 0 0
  1 commentaire
diadalina
diadalina le 7 Jan 2023
thank you so much here an other way to solve my problem :
n=5;
i=0;
H=zeros(n);
while i<=n
j=0;
i=i+1;
while j<=n
j=j+1;
if i+j-1<=n
H(i,j)=i+j-1 ;
end
end
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation 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!

Translated by