2 for loops and store output in a matrix to create 3D plot

Dear experts,
I have a question, i run 2 for loops(j and k) and I want to store my output(let's say A) in a matrix, and then make a 3D plot (A as a function of both j and k). How can I do it, since now the results A in a vector instead of matrix?
Thanks
Sharon

Answers (1)

I'm going to assume j and k looping from 1:J and 1:K and that you have stored your result in your loop with A(i,j)=SomeCalculation;. You can still use the plot3 if you have 3 vectors.
[X,Y] = meshgrid(1:J,1:K);%generate coordinate 'fields'
figure(1),clf(1)%get the focus on figure 1 and make it empty again
subplot(1,2,1)
surf(X,Y,A)%make a nice surface plot
xlabel('j'),ylabel('k')
subplot(1,2,2)
plot3(X(:),Y(:),A(:),'*b')%plot the results as scattered points
xlabel('j'),ylabel('k')
PS next time, first try something yourself and ask here to solve errors and unexpected behavior, that's how you learn ;)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 21 Feb 2017

Answered:

Rik
on 21 Feb 2017

Community Treasure Hunt

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

Start Hunting!