Saving For Loop Values Into a Matrix

1 vue (au cours des 30 derniers jours)
Jim Tom
Jim Tom le 24 Oct 2021
Commenté : Jim Tom le 24 Oct 2021
for i = 0:0.1:1
for j = 0:0.1:1
if (i^2 + j^2) <= 1
u = (sqrt(i^2 + j^2))^3;
else (i^2 + j^2) > 0
u = 3*sqrt(i^2 + j^2 - 1)+1;
end
end
end
I am looking to store these u values in a matrix u, because I want to plot the surface of u. I am having a hard time storing these values in a matrix. Thanks in advance. I only want to look at u values from 0-1 also if that helps at all.

Réponse acceptée

Image Analyst
Image Analyst le 24 Oct 2021
Try this:
alli = 0:0.1:1
allj = 0:0.1:1
for k1 = 1 : length(alli)
i = alli(k1);
for k2 = 1 : length(allj)
j = allj(k2);
if (i^2 + j^2) <= 1
u(k1, k2) = (sqrt(i^2 + j^2))^3;
elseif (i^2 + j^2) > 0
u(k1, k2) = 3*sqrt(i^2 + j^2 - 1)+1;
end
end
end
surf(u);
colorbar
  1 commentaire
Jim Tom
Jim Tom le 24 Oct 2021
Thanks this is great!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by