Plotting groups of different size separately, also error message: Vectors must be the same length
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
Accepted Answer
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 Comments
@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 :)
More Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)