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

1 view (last 30 days)
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')

Answers (2)

Torsten
Torsten on 23 Sep 2023
Edited: Torsten on 23 Sep 2023
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 Comment
stephanie
stephanie on 23 Sep 2023
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.

Sign in to comment.


Star Strider
Star Strider on 23 Sep 2023
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 Comments
stephanie
stephanie on 23 Sep 2023
Thank you so much for the help! This worked perfectly with what I was trying to accomplish!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!