이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I have a structure compiled from imported (x,y) datasets in the form of variable.dataset(i).x(:), variable.dataset(i).y(:) where the lengths of x&y are the same within the same dataset, but may differ between datasets i.e.:
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [3 4 5 6 7 8]';
variable.dataset(2).y = [1 9 2 8 3 7]';
is there a way to generate a single bar(x,y) figure containing all the x/y dataset pairs?
채택된 답변
As long as your x data does not contain duplicates:
% Data. Note the x has no duplicates.
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [5 6 7 8 9 10]';
variable.dataset(2).y = [1 9 2 8 3 7]';
bar(vertcat(variable.dataset(:).x),vertcat(variable.dataset(:).y))
If you meant that you want two bar plots on the same figure, do this:
bar(variable.dataset(1).x,variable.dataset(1).y)
hold on
bar(variable.dataset(2).x,variable.dataset(2).y,'r')
댓글 수: 8
in your example you modified dataset(2)'s range such that they don't overlap. if you use the original dataset, you get the error:
Error using bar (line 60)
XData cannot contain duplicate values.
Additionally, this method no longer maintains the integrity of dataset(1) compared to dataset(2).
Yes, as I said,
"As long as your x data does not contain duplicates."
Does the second example work for you? The second example I posted will work just fine with your original data. If you need it to all be one color instead of red and blue, just leave out the 'r' part.
Adam
2012년 9월 25일
In your later code, it plots the data on top of each other. This could allow for data to be masked by other datasets.
What i am looking to accomplish would need to be done with one bar() function call in order for the data to be place side by side instead of using hold which places them on top of each other.
any ideas?
Matt Fig
2012년 9월 25일
But let's think about this for a second. If in one data set you have y = 7 at x = 3 and at another you have y = 1 at x = 3, how could you possibly have only one bar represent both those things? It seems you either need to choose one set of data (my first example) or overlap them (my second example).
What does your third alternative actually look like? In other words, what do we see when we look for the height of the bar at x = 3?
I am not looking for one bar to represent both. Where there is overlap, i am looking for 2 bars next to each other; using your example: at x=3, one bar is length 1 and the other is length 7 both next to each other.
For locations where there isn't any overlap, i would expect the same place holders but a height of 0 for the non overlapping dataset.
Aha, now that I can work with! Here is one method:
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [3 4 5 6 7 8]';
variable.dataset(2).y = [1 9 2 8 3 7]';
MAT = nan(length(unique(vertcat(variable.dataset(:).x))),2);
MAT(variable.dataset(1).x,1) = variable.dataset(1).y;
MAT(variable.dataset(2).x,2) = variable.dataset(2).y;
bar(MAT)
If your real data has, say 4 instead of 2 sets, the replace the 2 in the first command with 4, and the two middle commands could be put into a loop.
for ii = 1:length(variable.dataset)
MAT(variable.dataset(ii).x,ii) = variable.dataset(ii).y;
end
I was using a somewhat trivial case for the example datasets - I don't believe this works if x is a noninteger i.e.
variable.dataset(1).x = (1:0.25:100)';
any ideas?
Hopefully you have left nothing out this time!
variable.dataset(1).x = [1 2 3 4]';
variable.dataset(1).y = [9 8 7 6]';
variable.dataset(2).x = [3 4 5 6 7 8]';
variable.dataset(2).y = [1 9 2 8 3 7]';
variable.dataset(3).x = [1.5 2.5 3.5].'; % Non-integers.
variable.dataset(3).y = [6 7 8].';
variable.dataset(4).x = [1.5 2.5 3.5 6.5].';
variable.dataset(4).y = [2 1 3 8].';
U = unique(vertcat(variable.dataset(:).x));
MY = nan(length(U),length(variable.dataset));
MX = repmat(U,1,size(MY,2));
for ii = 1:length(variable.dataset)
R = ismember(MX(:,1),variable.dataset(ii).x);
MY(R,ii) = variable.dataset(ii).y;
end
bar(MX,MY)
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기
제품
참고 항목
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)
