简体   繁体   中英

Efficient Data Structure for the average of a matrix

I have an array list.

Its value is

arrlist[0] = 1 2 3
arrlist[1] = 4 5 6
arrlist[2] = 7 8 9
arrlist[3] = 10 11 12
arrlist[4] = 13 14 15

WHat I want is getting the avg of : 1,4,7,10,13 . Then an avg of 2,5,8,11,14 and so on

and the resulting arraylist should contain only one string like 7,8,9 (the avg of all 5 columns )

The nos would be random.

Which would be the best efficient manner. ??

I thought of a method where I store every element in a new array list but the looping would be much big.

Can anyone suggestme an efficient way.

The way I am thinking is : Just a psuedo code

arraylist newarrylist = new arraylist ();
for(int j=0;j<arrlist.size*arrlist[0].size;j++) // as each arrlist would have same elements
{    
newarrylist[j] =  arrlist(j).sunstring(j); // means will get the substring, first column, then second..
}

Like people said, premature optimization is the root of all evil; unless of course your array is big (eg 10000 rows * 10000 columns).

In any case, there are not that many options in calculating averages!

What I'd do is something like this (take this more as a pseudo-code, I have not debugged it; also, make sure there are no integer overflows):

int[] columnAverages = new int[width];

for (int row = 0; row < height; row++)
   for (int column = 0; column < width; column++)
   {
      int value = (parse the next integer here);
      columnAverages[column] += value;
   }

for (int column = 0; column < width; column++)
   columnAverages[column] /= height;

Hope this helps

The data structure used in here is an array list (collection) of an array of integers.

There you go, a solution for your problem:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 
 * @author Mohamed Ennahdi El Idrissi
 * 25 February 2014.
 *
 */
public class User3345483 {

/*
 * arrlist[0] = 1   2    3
 * arrlist[1] = 4   5    6
 * arrlist[2] = 7   8    9
 * arrlist[3] = 10  11   12
 * arrlist[4] = 13  14   15
 */
static List<Integer[]> arrliste = new ArrayList<Integer[]>();
public static void main(String[] args) {

    /*
    arr = new Integer[]{1, 2, 3};
    arrliste.add(arr);
    arr = new Integer[]{4, 5, 6};
    arrliste.add(arr);
    arr = new Integer[]{7, 8, 9};
    arrliste.add(arr);
    arr = new Integer[]{10, 11, 12};
    arrliste.add(arr);
    arr = new Integer[]{13, 14, 15};
    arrliste.add(arr);
    */

    initArray();
    initArray();
    initArray();
    initArray();
    initArray();

    displayArray();

    Integer[]   arr;
    arr = new Integer[3];
    int avg;
    for (int i = 0; i < arr.length; i++) {
        avg = 0;
        for (int j = 0; j < arrliste.size(); j++) {
            avg += ((Integer[]) arrliste.get(j))[i];
        }
        avg = avg/arrliste.size();
        System.out.println("Average Column " + i + ": " +avg);
        arr[i] = avg;
    }
}

static void initArray() {
    Integer[] arr = new Integer[3];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = new Random().nextInt(100);
    }
    arrliste.add(arr);
}

static void displayArray() {
    System.out.println("Values of the Populated Array:");
    for (Integer[] integer : arrliste) {
        for (int i = 0; i < integer.length; i++) {
            System.out.print(integer[i] + "\t");
        }
        System.out.println();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM