user376089
user376089

Reputation: 1265

Axis labeling question

The x-axis and y-axis are interchanged in the figure by running the following matlab function I wrote.

Could anyone tell me where the problem is or help me fix it? Thanks in advance for any help.

function axislabeling(n)
x=1:1:n;
y=1:1:n;

z=zeros(n,n);

for i=1:n
    for j=1:n
        z(i,j)=i;
    end
end
surf(x,y,z(x,y))

xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')

Upvotes: 2

Views: 3779

Answers (3)

Sum
Sum

Reputation: 11

In matlab matrices are stored as (rows,column) format but rows are denoting y axis and columns are x axis. So, the plotting command instead of surf(x,y,z) it should be surf(x,y,z').

Upvotes: 1

Doresoom
Doresoom

Reputation: 7448

I agree with @walkytalky on this one.

For troubleshooting purposes, it may be better to use a case where x~=y to help you see things more clearly.

For example:

n=10;
x=1:n;  %# stepsize of 1 is default and need not be specified
y=x.^2; %# instead of y=1:n to more easily distinguish x and y
z=repmat(x',1,n) %# use of repmat should be faster than a nested loop
surf(x,y,z)
ylabel('y-axis')   
xlabel('x-axis')   
zlabel('z-axis')

gives a plot where the x and y axes are clearly labelled correctly.

Upvotes: 1

walkytalky
walkytalky

Reputation: 9543

I suspect the problem is not that the axes are mislabelled but that the graph is not what you expect. The reason is that matlab matrices are accessed (row, column) -- ie, (y,x) -- rather than (x,y) as you have it. So when you're setting z(i,j)=i you're getting the slope in the wrong direction.

Upvotes: 0

Related Questions