user2743295
user2743295

Reputation: 17

How to plot matrix vs matrix

I have two 3 x 4 matrices. I want to make a 2D plot of this but can't seem to plot it correctly. I am being shown multiple lines (but the plotting should be element by element).

Here is my code. Any suggestion?

X=[1 2 5 7; 2 4 6 1; 2 5 6 2]
Y=X.^0.5

plot(X(:),Y(:));

Edit just to be more clear, what I am trying to do here is plot X vs Y. taking into account the below example, when

 X= element(0,0); Y=element(0,0). 

similarly, when

X= element(0,1); Y=element(0,1)

and so on... Using this method, the graph will be plotted using those values (element by element) and we will get a 2D line plot of X vs Y=X.^0.5. I hope this clarifies what I am looking for.

X =

 1     2     5     7
 2     4     6     1
 2     5     6     2

Y =

1.0000    1.4142    2.2361    2.6458
1.4142    2.0000    2.4495    1.0000
1.4142    2.2361    2.4495    1.4142

Plot(X(:), Y(:)) may not be the right command so I am basically looking for edits in this.

Upvotes: 0

Views: 2220

Answers (3)

Stewie Griffin
Stewie Griffin

Reputation: 14939

I think the Daniel and Eitan have answered your question quite well, but here are some alternatives for you. I guess one of them should fit your needs:

If you just want a line representing X vs X^0.5:

X = linspace(0,7,100);
plot(X,X.^0.5)

or, if you want only the integer values of X and displayed as a scatter plot:

X = 0:7;
scatter(X, X.`0.5)

or, if you want to plot the matrices, element by element sorted:

plot(sort(X(:)),sort(Y(:)))

and as a scatter plot:

scatter(sort(X(:)),sort(Y(:)))

or, if you want it element by element, do as Eitan and Danial suggests:

scatter(X(:),Y(:))
plot(X(:),Y(:))

The last one will look like two lines, but it is really just one going back and forth.

Upvotes: 0

Daniel
Daniel

Reputation: 36710

If you don't want any lines, you have to set a line spec.

plot(X(:),Y(:),'x');

Upvotes: 2

Eitan T
Eitan T

Reputation: 32920

Are you looking for a scatter plot?

scatter(X(:), Y(:))

Upvotes: 4

Related Questions