Merging vectors and plotting
1 次查看(过去 30 天)
显示 更早的评论
T
2013-9-5
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?
采纳的回答
Walter Roberson
2013-9-5
unique([A;B])
16 个评论
T
2013-9-5
OK, what if A and B had y-values associated with it, would plotyy be the only option? Or can one still use unique([A;B]) and plot each function onto one graph?
Walter Roberson
2013-9-5
[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');
T
2013-9-6
What if, say, I have to represent Bvals using the stairs function? Is it legitimate if I use plotyy with the merged times but with two different functions with the same time vector?
Walter Roberson
2013-9-6
What would be the point?
plotyy(A, Avals, B, Bvals, @plot, @stairs)
Remember, plotyy uses a common x (time) axis.
T
2013-9-6
I am trying to truncate portions of data using data cursor. Getting a common time vector seems to be an issue for me. I already have the command you just wrote but I was wondering it could be done more simply.
Walter Roberson
2013-9-6
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
2013-9-6
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
2013-9-6
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
2013-9-6
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
2013-9-7
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
2013-9-7
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
2013-9-8
编辑:T
2013-9-8
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
2013-9-8
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
2013-9-8
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
2013-9-8
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Two y-axis 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)