Dear Matlab Community,
I have a matrix like
[24 80; 24 80; 24 80; 24 80; 30 120; 30 120; 48 124; 48 124; 48 124]
I need to have a plot that 80, 120, 124 be in the x axis and the numbers 4,2,3 be in the y axis.
We have 4 of 24, 3 of 30 and 3 of 48.
Thanks :)))

 채택된 답변

Voss
Voss 2024년 10월 3일

0 개 추천

M = [24 80; 24 80; 24 80; 24 80; 30 120; 30 120; 48 124; 48 124; 48 124];
[c,g] = groupcounts(M(:,2))
c = 3×1
4 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
g = 3×1
80 120 124
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
There are your y- and x-axis numbers.
Perhaps one of these plots is what you mean:
x = 1:numel(g);
figure('Position',[10 10 1000 400])
tiledlayout(2,2)
nexttile
plot(g,c)
nexttile
bar(g,c)
nexttile
plot(x,c)
xticks(x)
xticklabels(g)
nexttile
bar(x,c)
xticks(x)
xticklabels(g)

댓글 수: 4

Neda
Neda 2024년 10월 3일
Thank you Voss,
I did not know about groupcount. What is the meaning of M(:,2) in this command? I know M(:,2), but what is it role in groupcount?
Voss
Voss 2024년 10월 3일

M(:,2) is the second column of M. The code passes the second column of M to groupcounts().

https://www.mathworks.com/help/matlab/ref/double.groupcounts.html

Neda
Neda 2024년 10월 3일
Thank you so much !!!!
Voss
Voss 2024년 10월 3일
You're welcome!

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2024년 10월 2일

댓글:

2024년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by