二维数组和
我有如下所示的二维浮点数组,
{0.2,0.0,0.3,0.0,0.0}
{0.4,0.1,0.0,0.0,0.9}
{0.0,0.0,0.0,0.3,0.6}
我想得到以下输出
{0.6,0.0,0.3,0.0,0.0}
{0.6,0.1,0.0,0.0,1.5}
{0.0,0.0,0.0,0.3,1.5}
如果您进行分析,我将计算每列的非零值的总和,并用该总和值更新所有非零值。例如,在第一列中,我求和 (0.2+0.4=0.4) 并将两个值位置更新为 0.6。
我正在使用 Java,我该如何执行此操作?这是一个简单的例子,实时我有非常大的数组。
I have two dimensional float array as below
{0.2,0.0,0.3,0.0,0.0}
{0.4,0.1,0.0,0.0,0.9}
{0.0,0.0,0.0,0.3,0.6}
I want to get the following output
{0.6,0.0,0.3,0.0,0.0}
{0.6,0.1,0.0,0.0,1.5}
{0.0,0.0,0.0,0.3,1.5}
If you analyse, I sum each column's non zero value and update all non zero values with that sum value. For example, in first column I sum (0.2+0.4=0.4) and updated both value position with 0.6.
I am using Java, how can I perform this? Its a simple example, in real time I have really big arrays.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设它们的长度都相同,这是可行的。特殊情况可供读者练习。
这是它的结果:
This works assuming they're all the same length. Special cases are exercise to the reader.
And here's its result:
我们将输入数组命名为 float[][] a 并将并行输出数组命名为 b 并初始化为全零。
可能有一些更好的点你必须改变,比如浮点数和其他东西的比较,但我认为这个想法就在那里,
我认为它在 O(n*m) 中运行,这看起来不太好,但我试图保留迭代尽可能短。我没有看到任何更快的方法。即使有三个 for 循环,带有
k
的循环也只会为每个j
循环运行一次,因此渐近地它根本不会增加复杂性。Let's call your input array
float[][] a
and parallel output arrayb
initialized to all zeroes.There might be some finer points you have to change, such as comparison of the floats and stuff, but i think the idea is all there
I think it runs in O(n*m), andwhich doesnt seem great, but I tried to keep the iterations as short as possible. I dont see any faster way to do it. Even tho there are three for loops, the one with
k
will only run once for everyj
loop so asymptotically it doesnt increase complexity at all.