How do I make a bar graph from uneven arrays without adding zeros to the end of the smaller array?
2 次查看(过去 30 天)
显示 更早的评论
a1 = randi([0,800],1,2251);
a2 = randi([0,800],1,2347);
figure()
X1 = categorical({'red','green'});
X1 = reordercats(X1,{'red','green'});
Y1 = [a1;a2];
bar(X1,Y1);
xlabel({'Apples'},'FontWeight','bold');
ylabel({'Number of Apples'},'FontWeight','bold');
The problem comes in @ Y1. It wants to combine the two arrays, assuming because they are of different sizes. I am trying to create one row with 2 columns that are of different sizes. I don't think it is possible like that on MATLAB, but i do not want to have to add zeros to the end of the larger array just to be able to combine them. Is there an alternate way, possibly a loop, that would add zeros to the end of the smaller array everytime? That would be my next stab at trying to solve the problem. Any help is greatly appreciated!
0 个评论
采纳的回答
Star Strider
2023-2-1
I am not certain what you want.
This approach creates a NaN matrix with the column length of the larger vector, writes the original vectors to it, and then plots them. (I shortened the original vectors because the longer ones took too long (more than 55 seconds) to render here.)
% a1 = randi([0,800],1,2251);
% a2 = randi([0,800],1,2347);
a1 = randi([0,800],1,51);
a2 = randi([0,800],1,47);
maxlen = max(cellfun(@(x)size(x,2), {a1,a2}));
Y1 = NaN(2,maxlen);
Y1(1,1:numel(a1)) = a1;
Y1(2,1:numel(a2)) = a2;
Y1
figure()
X1 = categorical({'red','green'});
X1 = reordercats(X1,{'red','green'});
bar(X1,Y1);
xlabel({'Apples'},'FontWeight','bold');
ylabel({'Number of Apples'},'FontWeight','bold');
I doubt that a loop is necessary.
.
2 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!