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 件のコメント
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
2024 年 10 月 7 日
Apologies for not making that more clear in the original message. Thank you so much!
Star Strider
2024 年 10 月 7 日
As always, my pleasure!
No worries!
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
2024 年 10 月 7 日
That worked PERFECTLY haha!!
Thank you so much!
Star Strider
2024 年 10 月 7 日
As always, my pleasure!
その他の回答 (1 件)
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
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 についてさらに検索
参考
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)
