plot bar graph based on element type in matrix
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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개)
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
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)
