Getting Matrix Dimensions Error from Using "/", help with understanding problem?
古いコメントを表示
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 件)
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.
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)
totalbyintersection = rand(5,1)
totalintersection = totalbyhour + totalbyintersection
percentInter = 100 * (totalbyintersection ./ totalintersection) % Element-Wise Division
Using element-wise division eliminates the error, however I cannot determine if this is the desired result.
.
4 件のコメント
stephanie
2023 年 9 月 23 日
You probably will need to transpose ‘count’ in order for the bar chart to make sense.
Perhaps something like this —
count = poissrnd(2.5,24,3);
count = count.'; % Transpose
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')
.
stephanie
2023 年 9 月 23 日
Star Strider
2023 年 9 月 23 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
カテゴリ
ヘルプ センター および File Exchange で Pie Charts についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

