How can I get the sum of rows and columns of a table and add them to the table?
16 ビュー (過去 30 日間)
古いコメントを表示
I want to get the totals of each row and column from a table with values inserted by a user
This is my script so far:
row = input('Enter number of rows: ');
col = input('Enter number of columns: ');
for i = 1:row
for j = 1:col
str = ['Enter element in row ' num2str(i) ', col ' num2str(j) ': '];
A(i,j) = input(str);
end
end
A;
See the images for how I want it to look compared to how it looks now
0 件のコメント
採用された回答
KL
2017 年 11 月 7 日
編集済み: KL
2017 年 11 月 7 日
After you get the row and col values from the user, pre-allocate it by,
A = zeros(row+1,col+1);
I say "+1" because that'S where I want to store the sum. Now get the values from user as in the for loop, then to caluclate sum of all rows,
A(end,1:col) = sum(A(1:row,1:col),1); %last element will be 0
to sum all columns
A(1:row,end) = sum(A(1:row,1:col),2); %again, last element will be 0
final result will be like, (for a 3x3 input)
0.8673 0.8289 0.2804 1.9766
0.7212 0.7730 0.7949 2.2891
0.1438 0.3716 0.2151 0.7304
1.7322 1.9735 1.2904 0
5 件のコメント
Image Analyst
2017 年 11 月 8 日
That code still doesn't produce a uitable with any red and black borders, like you wanted and showed in the image you posted.
その他の回答 (1 件)
Image Analyst
2017 年 11 月 7 日
Not sure you can do that in MATLAB unless you try some undocumented calls to java. See http://undocumentedmatlab.com/
If you want to make it look like that in an Excel workbook, you can use ActiveX (Windows only) to have MATLAB tell Excel to format the cell shading and borders in any style you want. A generic ActiveX demo is attached.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!