二维数组的总和 [英] Sum of a two dimensional array
本文介绍了二维数组的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个2D数组L(i,j).如何求和所有取决于i
的元素,并将结果作为j
I have this 2D array L(i,j). How can I sum all the elements depending of i
and make the result as a function of j
我做到了:
do j=1,10
do i =1,30
T(j) = Sum( L(:,j)
end do
end do
可以吗?
推荐答案
几乎...您不使用i
(并且不需要),并且缺少一个括号:
Almost... you don't use i
(and you don't need to), and you are missing one bracket:
do j=1,10
T(j) = Sum( L(:,j) )
enddo ! j
您还可以在 sum
中使用维度参数来执行此操作一行操作:
You could also use the dimension parameter in sum
to do this operation in one line:
T = sum( L, dim=1 )
但是,我发现它很难阅读并且会坚持使用循环-在性能方面不应该有所不同.
However, I find that very difficult to read and would stick with the loop - it shouldn't make a difference in terms of performance.
这篇关于二维数组的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文