Getting Matrix Dimensions Error from Using "/", help with understanding problem?

2 次查看(过去 30 天)
totalbyhour = sum(count, 1);
totalbyintersection = sum(count, 2);
counttotal = sum(totalbyhour);
totalintersection = totalbyhour + totalbyintersection;
percentInter = 100 * (totalbyintersection / totalintersection); >>>>> getting "Error using / Matrix dimensions must agree." error
percentHr = 100 * (totalbyhour / totalintersection);
SectionLabels = {'Durango','IH10','LP410'};
figure
subplot(1,2,1)
bar(totalbyhour)
xlabel('hour')
ylabel('traffic')
legend('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24')
title('Bar Chart')
subplot
pie(totalbyintersection(1:3), SectionLabels)
title('Intersection totals')

回答(2 个)

Torsten
Torsten 2023-9-23
编辑:Torsten 2023-9-23
If count is a square matrix, use
percentHr = 100 * (totalbyhour ./ totalintersection.');
if you just want to divide each element of the vector totalbyhour by the corresponding element of the vector totalintersection.
  1 个评论
stephanie
stephanie 2023-9-23
Count is a 24x3 double that has 3 different intersection names for 24 hours; i am trying to get the percentage of the intersection totals to make up a pie chart and the traffic by each hour in a bar chart.

请先登录,再进行评论。


Star Strider
Star Strider 2023-9-23
Without knowing the sizes of the two arrays, one problem could be due to:
totalbyhour = sum(count, 1);
totalbyintersection = sum(count, 2);
Here, ‘totalbyhour’ is being calculated across the row, producing a row vector, while ‘totalbyintersection’ does the opposite, producing a column vector output. Adding them produces a matrix:
totalbyhour = rand(1,5)
totalbyhour = 1×5
0.3177 0.1562 0.7958 0.5226 0.3279
totalbyintersection = rand(5,1)
totalbyintersection = 5×1
0.0326 0.5358 0.4126 0.0508 0.0165
totalintersection = totalbyhour + totalbyintersection
totalintersection = 5×5
0.3503 0.1888 0.8285 0.5552 0.3605 0.8534 0.6919 1.3316 1.0584 0.8637 0.7302 0.5688 1.2084 0.9352 0.7405 0.3685 0.2070 0.8467 0.5735 0.3788 0.3342 0.1727 0.8124 0.5392 0.3445
percentInter = 100 * (totalbyintersection ./ totalintersection) % Element-Wise Division
percentInter = 5×5
9.3140 17.2800 3.9379 5.8759 9.0486 62.7791 77.4295 40.2346 50.6220 62.0324 56.5011 72.5418 34.1430 44.1186 55.7173 13.7989 24.5618 6.0056 8.8671 13.4246 4.9515 9.5805 2.0369 3.0692 4.8038
Using element-wise division eliminates the error, however I cannot determine if this is the desired result.
.
  4 个评论
stephanie
stephanie 2023-9-23
Thank you so much for the help! This worked perfectly with what I was trying to accomplish!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Pie Charts 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by