How can I plot vectors that are not of the same length in one plot so that they overlap?

10 views (last 30 days)
Hello everyone!
I have a problem plotting two vectors that are of different lengths (5 and 100) into one figure, so that they overlap. You might rephrase the problem in 'How can I, if possible at all, get two differently scaled x-axis in the same plot?' I already tried assigning different axis numbers, like
x1 = 0:1:5;
A1 %is a 5x1 vector
x2 = 0:20:200;
A2 %is a 100x1 vector
plot(x1,A1,x2,A2)
but that only got me the "Error using plot. Vectors must be the same length." message. (EDIT: I now learned, why, but still have the same problem, see attached file.) I also tried with hold:
x = 0:1:100;
A1, A2 %as before
plot(x, A1)
hold on
plot(x, A2)
hold off
which at least plotted the two graphs, but in the same scale (i.e. A2 the way it should be, but A1 only in the very small first 5% of the figure...) because it used the 1 to 100 x-axis that doesn't suit a vector that goes from 1 to 5. Does anyone know how to 'stretch' the vector or re-scale the axis or of any other solution to compare the plotted vectors?
Thanks in advance!
  2 Comments
Matt J
Matt J on 19 Nov 2017
Neither of these approaches should have worked. x has 101 elements which is different in length from both A1 and A2.
Matt J
Matt J on 19 Nov 2017
Note that x1 as you have defined it in your first approach is not of length 5 to match A1, nor is x2 of length 100 to match A2.
>> x1=0:1:5; x2=0:20:200; whos x1 x2
Name Size Bytes Class Attributes
x1 1x6 48 double
x2 1x11 88 double

Sign in to comment.

Answers (3)

Matt J
Matt J on 19 Nov 2017
Edited: Matt J on 19 Nov 2017
How about
semilogx( 1:numel(A1), A1, 1:numel(A2), A2);
  2 Comments
Anna M
Anna M on 19 Nov 2017
Thanks for the idea (though I don't exactly know what semilogx is because I don't have much experience with coding in MatLab), but that also returned me the 'Error using semilogx. Vectors must be the same lenght.' message...
Matt J
Matt J on 19 Nov 2017
Edited: Matt J on 19 Nov 2017
though I don't exactly know what semilogx is because I don't have much experience with coding in MatLab
The doc command will help with that
>> doc semilogx
but that also returned me the 'Error using semilogx. Vectors must be the same lenght.' message...
No reason it should have. Worked fine for me.
If you are not using 1:numel(A1) as in my example, make sure your x vectors are the same length as their corresponding A vectors.

Sign in to comment.


Anna M
Anna M on 19 Nov 2017
Edited: Anna M on 20 Nov 2017
Okay, now I get why the error occured (overlooked that 0 is also an element of 0:1:100), and that you recommended semilogx for the different scaling on the x-axis (thanks for that), but it did not exactly do what I wanted it to. The graphs still don't overlap in a way to be directly comparable. And using 'plot' again with the correct x-vector still displays the graphs according to their length (like in the beginning, see attached files). These are the result of using the line you suggested on my example and using plot instead of semilogx.
Are there any other ideas?
  1 Comment
Matt J
Matt J on 20 Nov 2017
It's not clear to me how they are meant to be compared. To be comparable, they surely have to be on a common x-axis, and if they are, they surely cannot "overlap" because you don't have the same x-data available for each.

Sign in to comment.


Anna M
Anna M on 21 Nov 2017
Thanks for your help, anyway. I now realized how easily I could have done what I was trying if I just had used 100/4 intervals instead of the failed try with 100/5... I guess that would have been too boring ;)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!