Saving For Loop Values Into a Matrix

1 Ansicht (letzte 30 Tage)
Jim Tom
Jim Tom am 24 Okt. 2021
Kommentiert: Jim Tom am 24 Okt. 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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 24 Okt. 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

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by