二维数组和

发布于 2024-10-28 02:17:29 字数 360 浏览 1 评论 0原文

我有如下所示的二维浮点数组,

{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-11-04 02:17:29

假设它们的长度都相同,这是可行的。特殊情况可供读者练习。

class MatTest {
    static void makeSums(float[][] floats) {
        // we wouldn't be doing any operations on these inputs anyway, so return
        if(floats == null || floats.length == 0 || floats.length == 1) return;

        // check to make sure it's retangular
        for(float[] arr : floats) {
            if(arr.length != floats[0].length) {
                throw new IllegalArgumentException("makeSums() requires rectangular array");
            }
        }

        for(int i = 0; i < floats[0].length; i++) {
            // do each column
            float sum = 0f;
            for(int j = 0; j < floats.length; j++) {
                sum += floats[j][i];
            }
            for(int j = 0; j < floats.length; j++) {
                if(floats[j][i] != 0) floats[j][i] = sum;
            }
        }
    }

    public static void main(String[] args) {
        float[][] floats = new float[3][5];
        floats[0] = new float[] {0.2f,0.0f,0.3f,0.0f,0.0f};
        floats[1] = new float[] {0.4f,0.1f,0.0f,0.0f,0.9f};
        floats[2] = new float[] {0.0f,0.0f,0.0f,0.3f,0.6f};

        makeSums(floats);

        for(int i = 0; i < floats.length; i++) {
            for(int j = 0; j < floats[0].length; j++) {
                System.out.print(floats[i][j]);
                System.out.print(" ");
            }
            System.out.println(" ");
        }
    }
}

这是它的结果:

 C:\Documents and Settings\glow\My Documents>javac MatTest.java

 C:\Documents and Settings\glow\My Documents>java MatTest
 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

This works assuming they're all the same length. Special cases are exercise to the reader.

class MatTest {
    static void makeSums(float[][] floats) {
        // we wouldn't be doing any operations on these inputs anyway, so return
        if(floats == null || floats.length == 0 || floats.length == 1) return;

        // check to make sure it's retangular
        for(float[] arr : floats) {
            if(arr.length != floats[0].length) {
                throw new IllegalArgumentException("makeSums() requires rectangular array");
            }
        }

        for(int i = 0; i < floats[0].length; i++) {
            // do each column
            float sum = 0f;
            for(int j = 0; j < floats.length; j++) {
                sum += floats[j][i];
            }
            for(int j = 0; j < floats.length; j++) {
                if(floats[j][i] != 0) floats[j][i] = sum;
            }
        }
    }

    public static void main(String[] args) {
        float[][] floats = new float[3][5];
        floats[0] = new float[] {0.2f,0.0f,0.3f,0.0f,0.0f};
        floats[1] = new float[] {0.4f,0.1f,0.0f,0.0f,0.9f};
        floats[2] = new float[] {0.0f,0.0f,0.0f,0.3f,0.6f};

        makeSums(floats);

        for(int i = 0; i < floats.length; i++) {
            for(int j = 0; j < floats[0].length; j++) {
                System.out.print(floats[i][j]);
                System.out.print(" ");
            }
            System.out.println(" ");
        }
    }
}

And here's its result:

 C:\Documents and Settings\glow\My Documents>javac MatTest.java

 C:\Documents and Settings\glow\My Documents>java MatTest
 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
染年凉城似染瑾 2024-11-04 02:17:29

我们将输入数组命名为 float[][] a 并将并行输出数组命名为 b 并初始化为全零。

float curSum = 0.0;   
first = true;
for(int i = 0; i < a[0].length; i++)
{
    for(int j = 0; j < a.length; j++)
    { 
         if(a[i][j] != 0)
         {
             if (first)
             {
                 for(int k = j; k < a.length; k++)
                     curSum += a[i][k];
                 first = false;
             }
             b[i][j] = curSum;
         }
     }
     curSum = 0.0;
     first = true;
}

可能有一些更好的点你必须改变,比如浮点数和其他东西的比较,但我认为这个想法就在那里,

我认为它在 O(n*m) 中运行,这看起来不太好,但我试图保留迭代尽可能短。我没有看到任何更快的方法。即使有三个 for 循环,带有 k 的循环也只会为每个 j 循环运行一次,因此渐近地它根本不会增加复杂性。

Let's call your input array float[][] a and parallel output array b initialized to all zeroes.

float curSum = 0.0;   
first = true;
for(int i = 0; i < a[0].length; i++)
{
    for(int j = 0; j < a.length; j++)
    { 
         if(a[i][j] != 0)
         {
             if (first)
             {
                 for(int k = j; k < a.length; k++)
                     curSum += a[i][k];
                 first = false;
             }
             b[i][j] = curSum;
         }
     }
     curSum = 0.0;
     first = true;
}

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 every j loop so asymptotically it doesnt increase complexity at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文