显示 更早的评论
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.
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?
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)
