Saving For Loop Values Into a Matrix

1 visualización (últimos 30 días)
Jim Tom
Jim Tom el 24 de Oct. de 2021
Comentada: Jim Tom el 24 de Oct. de 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.

Respuesta aceptada

Image Analyst
Image Analyst el 24 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by