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
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)
EDIT — Forgot about the bar plots. Now added.
.

댓글 수: 8

Kitt
Kitt 2024년 10월 7일
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.
.
Kitt
Kitt 2024년 10월 7일
Apologies for not making that more clear in the original message. Thank you so much!
As always, my pleasure!
No worries!
Kitt
Kitt 2024년 10월 7일
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.)
.
Kitt
Kitt 2024년 10월 7일
That worked PERFECTLY haha!!
Thank you so much!
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2024년 10월 7일
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

Kitt
Kitt 2024년 10월 7일
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에 대해 자세히 알아보기

질문:

2024년 10월 7일

댓글:

2024년 10월 7일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by