You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Merging vectors and plotting
2 views (last 30 days)
Show older comments
Suppose you have the following time vectors in seconds:
A =
0
1
2
3
4
5
6
7
8
9
10
and
B =
3.1
3.6
3.7
4.1
4.3
4.6
4.8
5.1
5.4
5.8
6.7
6.8
6.8
Can one merge A and B to establish one time vector? What if there are values associated with each element in A and B? Or does one have to plot each separetly using plotyy?
Accepted Answer
Walter Roberson
on 5 Sep 2013
unique([A;B])
16 Comments
Walter Roberson
on 5 Sep 2013
[mergedtimes, ia] = unique([A;B]);
ABvals = [Avals; Bvals];
mergedvals = ABvals(ia);
Note that in this case if there are multiple entries with the same time, then which value will be picked up might well not be the one you would prefer.
If you are looking for all the unique pairs, then
AB = [A, Avals; B, Bvals];
uAB = unique(AB, 'rows');
Walter Roberson
on 6 Sep 2013
What would be the point?
plotyy(A, Avals, B, Bvals, @plot, @stairs)
Remember, plotyy uses a common x (time) axis.
Walter Roberson
on 6 Sep 2013
plotyy() uses a common time vector.
My guess is that what you want to do is to be able find the A value and the B value both at a given time, even though there might not be any samples of one or the other (or both?) at that particular time. If that is what you are trying to do then you should be creating a common time vector and then using interp()
mergedtimes = unique([A;B]);
Amergedvals = interp(A, Avals, mergedtimes);
Bmergedvals = interp(B, Bvals, mergedtimes);
You might not want the default interpolation scheme for interp() though. And if the times on one extend before or after the times on the other, then you need to decide what you want to have as the interpolated result of the other for those times.
T
on 6 Sep 2013
Yes that's what I want to do.
You mean interp1 right?
My concern is that when you interpolate the B, where B is the stairs, it loses the original display and I haven't even truncated the data yet and one gets a saw-tooth looking function.
T
on 6 Sep 2013
Actually, suppose I want to interp this vector:
value =
0
0.23
0.23
0.55
0.55
0.97
1.03
1.05
0.32
0.41
0
time_seconds=
7877
7886
7930
7937
7946
7954
7962
7968
7984
7988
8015
So if one looks at the first two elements of the time vector:
7877,7886. Can one interpolate the value associated with 7877, in this case, 0 up until 7886? And likewise for the other elements? Maybe this would work.
Walter Roberson
on 6 Sep 2013
Yes, that is what interp1() would do. You might specify linear interpolation or nearest interpolation. If you want "last value not greater than this point" then histc() is usually an easier route.
timespacing = 0.1; %adjust this as needed!
mintime = mergedtimes(1);
maxtime = mergedtimes(2);
sample_at_times = mintime : timespacing : maxtime;
[counts, Abinidx] = histc( sample_at_times, [-inf, Atimes, inf] );
[counts, Bbinidx] = histc( sample_at_times, [-inf, Btimes, inf] );
A_held = nan(size(sample_at_times));
B_held = nan(size(sample_at_times));
Amask = Abinidx > 1 & Abinidx <= length(Atimes) + 1; times within A
Bmask = Bbinidx > 1 & Bbinidx <= length(Btimes) + 1;
A_held(Amask) = Atimes(Abinidx(Amask)-1);
B_held(Bmask) = Btimes(Bbinidx(Bmask)-1);
plot(sample_at_times, A_held, 'bo-', sample_at_times, B_held, 'gs-');
T
on 7 Sep 2013
Okay. Now my concern is doing the same for the y-values for one of the vectors with that small amount of time.
value =
0
0.23
0.23
0.55
0.55
0.97
1.03
1.05
0.32
0.41
0
time_seconds=
7877
7886
7930
7937
7946
7954
7962
7968
7984
7988
8015
I can't use histc in this case.
Essentially, likewise with Amask or Bmask, the actual value shall remain constant until it hits those specified times.
Walter Roberson
on 7 Sep 2013
Small change to what I had posted:
A_held(Amask) = Avals(Abinidx(Amask)-1);
B_held(Bmask) = Bvals(Bbinidx(Bmask)-1);
then exactly the same technique can be used for your y.
T
on 8 Sep 2013
Edited: T
on 8 Sep 2013
What you've done with histc was just took the time with the largest dim, and binned them. Then with filled in empty elements to match the same size of both vectors.
But for instance, at times: 16 28
the y-value would be 0 until 28, then it should be 0.23 and so on.
How do you repeat elements in a vector until the next time associated with it?
Walter Roberson
on 8 Sep 2013
mergedtimes = unique([A;B;time_seconds]);
timespacing = 0.1; %adjust this as needed!
mintime = mergedtimes(1);
maxtime = mergedtimes(2);
sample_at_times = mintime : timespacing : maxtime;
[counts, Abinidx] = histc( sample_at_times, [-inf, Atimes, inf] );
[counts, Bbinidx] = histc( sample_at_times, [-inf, Btimes, inf] );
[counts, ybinidx] = histc( sample_at_times, [-inf, time_seconds, inf] );
A_held = zeros(size(sample_at_times));
B_held = zeros(size(sample_at_times));
y_held = zeros(size(sample_at_times));
Amask = Abinidx > 1 & Abinidx <= length(Atimes) + 1; times within A
Bmask = Bbinidx > 1 & Bbinidx <= length(Btimes) + 1;
ymask = ybinidx > 1 & ybinidx <= length(time_seconds) + 1;
A_held(Amask) = Avals(Abinidx(Amask)-1);
B_held(Bmask) = Bvals(Bbinidx(Bmask)-1);
y_held(ymask) = value(ybinidx(ymask)-1);
Now take a closer look at those last three and see that the values being written into the "held" variables are not the time values, but are instead the A, B, or y value. And everything before the first relevant time for each will be 0, and everything after the first relevant time for each will be 0 (you didn't say what to do at the end.)
T
on 8 Sep 2013
So now that we have a common time axis, suppose that function B needs to be set at some later time called time_t0.
In using plotyy, I had what you mentioned in your earlier post:
plotyy(A, Avals, B, Bvals, @plot, @stairs)
But now that we have a common time vector, how do we adjust, say, B while still maintaining the same time axis?? Where B represents the time value of Bvals.
Walter Roberson
on 8 Sep 2013
Unless your vectors are much longer than you show, the easiest thing would be to re-run the B portion of above after adding the new time and value to the Btimes and Bvals. If the new time is after all of the old times, the easiest thing to do is rerun all of the above.
More Answers (0)
See Also
Categories
Find more on Two y-axis in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)