Scala中数组的按元素求和 [英] Element-wise sum of arrays in Scala

查看:867
本文介绍了Scala中数组的按元素求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算数组的按元素求和?

How do I compute element-wise sum of the Arrays?

val a = new Array[Int](5)
val b = new Array[Int](5)
// assign values
// desired output: Array -> [a(0)+b(0), a(1)+b(1), a(2)+b(2), a(3)+b(3), a(4)+b(4)]

a.zip(b).flatMap(_._1+_._2)

缺少扩展功能的参数类型

missing parameter type for expanded function

推荐答案

尝试:

a.zip(b).map { case (x, y) => x + y }

这篇关于Scala中数组的按元素求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆