現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
Adam
2012 年 9 月 25 日
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!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)
