Saving For Loop Values Into a Matrix

1 view (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!