Plotting groups of different size separately, also error message: Vectors must be the same length
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello!
I tried plotting some data and got the 'Error using plot. Vectors must be the same length.' error message.
I am trying to plot two sets of data:
G: 0.66, 0.05, 0.51, 0.4
B: 0.15, 0.03, 0.10, 0.62, 0.002, 0.10, 0.07, 0.2
using the following line of code: plot([1 2],[G; B], 'MarkerSize',15)
I know the error message is because the two groups are of different sizes, but is there a way to tell matlab that they are not paired, I just want them separate, but I want them plotted in that format?
Thanks!
Vyte
採用された回答
You have 2 values for x (1 and 2) but you have 4 values in the G vector and 8 values in the B vector. Please give a complete list of all (x,y) coordinates that you'd like to plot.
G = [0.66, 0.05, 0.51, 0.4] % A 1 by 4 row vector.
G = 1×4
0.6600 0.0500 0.5100 0.4000
B = [0.15, 0.03, 0.10, 0.62, 0.002, 0.10, 0.07, 0.2] % A 1 by 8 row vector.
B = 1×8
0.1500 0.0300 0.1000 0.6200 0.0020 0.1000 0.0700 0.2000
y = [G; B] % Concatenate B below G vertically. (Won't work because # columns is different in the two row vectors).
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Dimensions of arrays being concatenated are not consistent.
plot([1 2],[G; B], 'MarkerSize',15)
9 件のコメント
@Image Analyst maybe this will help, this is an example of what I am trying to plot. Just data points in space, and in separate parts of the graph.
I will always have a different number of G vs B, because I have less good outcome patients.
Is there another way to plot this data?

I thought I solved that for you in your related question:
@Image Analyst Yes exactly its very similar!
In the previous question that was paired regions, so both x and y had identical number of values, and they needed to be joined by a line.
This time, I will never have equal number of values (B will always be greater then G) and they are not paired. I cant seem to find a function that would allow me to plot them in that exact format as in: https://www.mathworks.com/matlabcentral/answers/1795290-plotting-data-on-binary-axis-and-draw-lines-between-points#answer_1042270
but of different size groups.
I came across this post: https://tex.stackexchange.com/questions/460722/how-to-make-a-categorical-grouped-scatterplot called Categorical Grouped Scatterplot, but again they seemed to be using an equal number of variables.
Is it only possible to plot groups with equal numbers?
This is what I get if I plot G and B separately using 'hold on' function, the problem is they cover each other so I cant see the overall distribution in the two groups. And the legend isnt working.

markerSize = 40;
G = [0.66, 0.05, 0.51, 0.4]; % A 1 by 4 row vector.
B = [0.15, 0.03, 0.10, 0.62, 0.002, 0.10, 0.07, 0.2]; % A 1 by 8 row vector.
plot(ones(1, numel(G)), G, '.', 'MarkerSize', markerSize)
hold on
plot(2 * ones(1, numel(B)), B, '.', 'MarkerSize', markerSize)
xlim([0, 2.5])
grid on;
xticklabels({'', '', 'G', '', 'B'})

@Image Analyst Thats perfect!! exactly what I was looking for!

If its not too much can I ask how does plot(ones(1, numel(G)), G actually work?
I understand numel(G) extarcts the size of the variable G which is 7 and those values are plotted as '1' using G as a vector in space? so B is done in the same way but by multiplying the vector space by 2 it shifts the whole graph to the right.
Thank you very much for your help!
For every y value you must have an x value so the ones just makes sure of that. It creates a vector of all 1's since all of the G's are to be plotted with the same x value, not different ones. Same for B -- I made it 2 so it's apart from the G dots.
Makes sense! Thank you for your help and the explanation :)
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
参考
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)
