sum all columns in a matrix
10 ビュー (過去 30 日間)
古いコメントを表示
hi,
i want to sum all the columns of some matrix.
but without using a loop or the sum function.
for ex:
for the matrix [1 3 2; 4 4 8]
i will recive [5 7 10]
0 件のコメント
回答 (2 件)
Maneet Kaur Bagga
2022 年 7 月 5 日
As per my understanding you want to get the sum of the matrix coloumn wise so you can use the MATLAB sum function. All the MATLAB functions by default work on coloumn so you get the sum directly. You can refer to the code below. Hope it helps!
x = [1 3 2; 4 4 8]
sum(x)
2 件のコメント
DGM
2022 年 7 月 5 日
In any practical scenario, using sum() would be perfectly sensible, but OP was working around contrived limitations -- no loops and no sum().
Maneet Kaur Bagga
2022 年 7 月 5 日
You can refer to the below code as a solution.
x1 = [1 2 3; 4 5 6; 7 8 9];
all =(x1);
sumcol=0;
for i=1:length(all)
sumcol = 0;
for j = 1:length(all)
sumcol = sumcol + all(j,i);
end
fprintf("%d ",sumcol);
end
参考
カテゴリ
Help Center および File Exchange で Introduction to Installation and Licensing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!