Clear Filters
Clear Filters

plotting two-vectors on same axis

3 views (last 30 days)
I have vector x contains 33-elements
x = [
1.0000
0.9974
0.9853
0.9793
0.9735
0.9588
0.9560
0.9461
0.9421
0.9385
0.9380
0.9372
0.9343
0.9333
0.9319
0.9306
0.9286
0.9280
0.9969
0.9933
0.9926
0.9920
0.9817
0.9751
0.9718
0.9572
0.9552
0.9462
0.9397
0.9362
0.9321
0.9291
0.9288]
>> t=[1:33];
>> plot(t,x)
gives you a graph, now i have a y vector which contains only 13 elements
y = [
1.0000
0.9974
0.9853
0.9712
0.9588
0.9360
0.9333
0.9295
0.9928
0.9763
0.9483
0.9397
0.9279]
now i wanted to draw graph on existing one such that elements values of y, which are near to x values should come same t-axis points. example:- y(4)=0.9712 is almost equal equal to x(5)=0.9735 at t=5, y(end)=0.9279 is equal to x(end)=0.9288 at t=33... hence i want y(4),y(end) on t=5,t=33.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Jun 2015
y(end)=0.9279 is equal to x(end)=0.9288 at t=33
0.9729 is smaller than any x value, and is closest to x(18) which is 0.9280.
y(4)=0.9712 is almost equal equal to x(5)=0.9735 at t=5
0.9712 is closer to x(25) = 0.9718
The descriptive part about "near to x values" leads to this code:
plot(1:length(x),x,'r',interp1(x, 1:length(x), y, 'nearest', 'extrap'),y,'g.-')
which has each y value being drawn at the t value that corresponds to the x value that is closest to the y value.
Your rules about what is near enough do not appear to be well defined.
  1 Comment
Raghavendra Reddy P
Raghavendra Reddy P on 1 Jun 2015
Thank you sir, since the above values are small fraction numbers i couldn't differentiate exactly the way i wanted. your coding is fine. i just wanted to draw y graph on same x-axis of x graph such that elements values of y and x are closer.

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!