plot bar graph based on element type in matrix
显示 更早的评论
I have a 20x100 (t,N) matrix with each element being either 1, 2, or 3. I want to create a bar graph showing the amount of each type of element. so at t=20, of the 100 columns, how many have 1, how many have 2, how many have 3.
Is that possible?
采纳的回答
Star Strider
2024-10-7
编辑:Star Strider
2024-10-7
Do you want all of them, or just the last row (t=10)?
This does both —
A = randi(3, 20, 100)
A = 20×100
2 2 1 3 1 3 3 3 1 1 1 2 2 2 1 3 3 1 1 1 2 3 3 1 2 1 2 1 2 1
2 2 3 3 1 1 3 3 3 2 3 1 1 3 2 1 1 1 2 2 1 2 1 1 3 2 3 3 3 2
3 2 3 2 2 1 3 1 2 3 3 3 3 2 2 2 1 1 2 3 2 2 3 1 1 1 1 1 1 1
2 1 3 3 3 1 1 1 3 1 3 1 2 1 1 1 1 1 1 3 1 2 2 3 3 1 3 1 1 1
2 3 3 1 3 2 1 3 1 2 3 1 2 2 1 1 1 1 2 3 1 1 1 3 2 3 1 2 3 3
1 2 2 2 2 2 1 3 2 3 3 3 3 2 2 2 3 3 2 2 3 1 2 1 1 2 3 1 1 2
3 1 2 3 2 2 3 2 3 2 2 1 1 2 3 1 3 3 1 3 2 1 3 1 1 1 3 3 2 1
1 2 2 3 1 3 1 1 3 2 3 3 3 1 3 2 1 3 3 1 3 2 2 3 3 2 2 3 1 3
1 3 1 2 2 3 2 1 3 1 2 2 2 1 1 2 2 3 2 1 1 3 3 3 1 2 3 1 3 1
1 1 3 1 2 1 1 3 2 2 3 3 2 3 2 3 1 2 1 3 1 2 1 2 1 2 1 1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
TallyAll = accumarray(A(:), 1)
TallyAll = 3×1
675
645
680
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ResultAll = table(TallyAll, 'RowNames',compose('%d',1:3))
ResultAll = 3x1 table
TallyAll
________
1 675
2 645
3 680
figure
bar(1:3, TallyAll)

Tally20 = accumarray(A(20,:).', 1)
Tally20 = 3×1
35
30
35
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Result20 = table(Tally20, 'RowNames',compose('%d',1:3))
Result20 = 3x1 table
Tally20
_______
1 35
2 30
3 35
figure
bar(1:3, Tally20)

.
8 个评论
In the end I'd like to see all of them, separated. 1, 2, and 3 all represent a decision an individual (N) can make, and I want to see the change in distribution of decisions over time (t). So like at time t = 1, 30 individuals chose 1, 40 individuals chose 2, and 30 individuals chose 3. And then do that for all 20 timesteps.
O.K. That was not initially obvious.
Two options —
A = randi(3, 20, 100)
A = 20×100
3 1 3 1 2 1 2 3 3 3 2 1 1 2 2 1 2 2 3 2 1 3 2 3 2 2 2 3 2 2
3 1 2 3 2 1 2 3 1 3 2 3 2 2 3 2 2 3 1 2 3 1 2 2 3 3 1 1 1 3
1 3 2 2 2 2 1 1 3 1 2 1 2 2 1 2 1 2 2 2 2 2 3 1 3 1 3 3 1 2
1 1 1 3 3 3 1 2 3 1 3 1 1 1 1 3 1 3 1 3 1 3 1 3 1 3 1 1 2 3
2 3 3 3 1 1 1 3 1 2 2 3 1 2 2 1 1 2 1 2 2 2 2 2 2 2 2 2 1 2
1 1 2 1 2 3 3 2 2 3 3 3 1 2 2 2 1 2 3 2 3 1 1 2 3 2 2 3 3 2
2 3 3 3 1 1 2 2 3 3 3 1 1 2 2 1 2 1 1 1 1 1 2 3 1 1 2 2 3 1
2 1 3 2 3 3 3 2 1 3 2 3 2 2 3 1 2 3 2 1 3 2 2 2 3 2 3 3 2 2
2 1 2 1 1 3 3 3 1 3 3 3 1 2 1 1 3 2 3 1 3 2 1 3 3 1 1 1 3 3
1 2 3 3 1 3 3 2 1 3 3 3 2 3 3 1 2 1 3 1 2 3 1 2 3 2 2 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for k = 1:size(A,1)
Tally(:,k) = accumarray(A(k,:).', 1);
end
figure
tiledlayout(5,4, TileIndexing='columnmajor')
for k = 1:size(Tally,2)
nexttile
bar(1:3, Tally(:,k))
grid
title("Row "+k)
end

figure
bar3(1:3, Tally)
xlabel('Rows')
ylabel('Responses')

The first option presents each result in a separate plot, the second option presents all of them in a single 3D plot.
.
Apologies for not making that more clear in the original message. Thank you so much!
As always, my pleasure!
No worries!
Sorry to call on you again, but I keep getting an error
"Unable to perform assignment because the size of the left side is 2-by-1 and the size of the right side is
3-by-1.
Tally(:,k) = accumarray(optchosen(k,:).', 1);"
I have k as 1:size(optchosen,1)
I'm not sure how I'm getting this error.
It might be because I realized the last timestep is actually all 0, so elements can be either 0, 1, 2, or 3. Though I don't see how that influences this first line of code.
No worries.
Your ‘optchosen’ variable needs to be a (20x100) matrix with all three choices present in each row to work wiith my original code. There may be only two choices in some rows, and that would definitely not work with my code, since it depends on every row having all three choices.
One possibiility is to preallocate the ‘Tally’ matrix, and additionally be certain that the choices are allocated correctly in each column —
A = randi(3, 20, 100)
A = 20×100
2 2 1 1 3 3 3 2 2 1 2 1 1 3 3 3 3 3 2 3 3 3 1 1 2 1 3 1 2 3
3 2 1 2 2 3 1 3 1 1 1 1 2 1 3 2 3 1 1 2 3 3 1 3 2 1 2 1 3 1
2 1 1 1 3 2 3 2 3 3 1 2 1 1 3 2 3 3 1 3 2 3 2 3 2 2 1 2 3 2
3 3 1 3 3 1 1 2 3 1 3 2 1 2 3 3 1 1 2 1 2 3 2 2 3 1 2 3 1 3
1 2 3 1 3 1 2 2 3 2 1 3 3 3 3 3 1 2 1 2 3 3 2 1 3 3 2 1 2 2
2 1 1 2 1 3 1 3 2 1 1 3 2 3 1 2 2 3 2 2 2 2 1 2 3 1 3 2 2 3
2 1 1 3 2 1 1 2 1 2 3 3 2 2 1 1 1 2 3 3 3 3 2 3 2 1 1 2 2 3
2 2 1 3 2 3 3 3 1 1 2 1 2 1 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 3
1 1 3 1 3 1 3 3 2 1 3 2 2 3 3 2 2 1 2 3 3 3 3 2 3 3 2 2 3 3
3 1 3 1 1 1 3 3 3 3 2 3 3 2 3 3 3 3 3 3 1 3 3 1 3 2 1 2 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(5,:) = randi(2, 1, 100); % Test #1 (No 3’s)
A(10,:) = randi([2 3], 1, 100); % Test #2 (No 1’s)
A(15,A(15,:) == 2) = 3; % Test #3 (No 2’s)
Tally = zeros(3, size(A,1));
for k = 1:size(A,1)
Uchoice = unique(A(k,:));
Count = accumarray(A(k,:).', 1); % Raw Count Veector
Tally(Uchoice,k) = Count(Count ~= 0); % Allocate ‘Tally’ Results To Non-Zero Rows
end
figure
tiledlayout(5,4, TileIndexing='columnmajor')
for k = 1:size(Tally,2)
nexttile
bar(1:3, Tally(:,k))
grid
title("Row "+k)
end

figure
bar3(1:3, Tally)
xlabel('Rows')
ylabel('Responses')

That should allocate the choices correctly, and be certain that every column has three rows, even if some contain zero counts. (This worked when I tested it.)
.
That worked PERFECTLY haha!!
Thank you so much!
As always, my pleasure!
更多回答(1 个)
M=randi([1 3],20,100);
whos t
[min(M(:)) max(M(:))]
ans = 1×2
1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
histogram(M(20,:))
xticks(1:3)
xlabel('Bin'), ylabel('Count')
title('Counts for t=20')

1 个评论
I've tried the histogram but the problem is I want to see the distribution over time, and when I try to plot multiple histograms they are just on top of each other and I can't really see the change
类别
在 帮助中心 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
另请参阅
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)
