이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Merging vectors and plotting
조회 수: 2 (최근 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 (한국어)