現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)