开发者

two dimensional array sum

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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜