Find average of List<Point> in Java - java

I have a List variable 'l' which has values like this:
List<Point> l = new ArrayList<Point>();
Imgproc.convexHull(contours,hull);
for (int j = 0; j < intlist.length; j++) {
l.add(contours.toList().get(hull.toList().get(j)));
}
After this I am getting following values of l:
l = [{1,2}, {3,4}, {5,6}]
and so on. How to find the average of 1st and 2nd values in l.get(i) for all i, as an array. For example, for the above I want the following:
x = 3
y = 4

Finally I got the answer! Below code will do the job:
double sum_x = 0;
double sum_y = 0;
int j;
for (j = 0; j < hull.size().height; j++) {
l.add(contours.toList().get(hull.toList().get(j)));
sum_x += l.get(j).x;
sum_y += l.get(j).y;
}
x = sum_x / j;
y = sum_y / j;

Related

How to increment an array with a nested for loop?

There must be a more elegant way to do the following:
for(int i = 0; i < subsets.length-10; i++) {
sample = (int)subsets[i];
int y = (sample
+ ((int)(subsets[i+1]))
+ ((int)(subsets[i+2]))
+ ((int)(subsets[i+3]))
+ ((int)(subsets[i+4]))
+ ((int)(subsets[i+5]))) / 6;
}
Basically, it adds the next 5 values for 'subsets' incrementally. How to use a nested for loop to use a single formula that means I can increase how many times it adds the next value and then divide by that many also to get an average.
Something like?:
y = (sample + ((int)(subsets[i++n]))/n
where n is number of times it incrementally adds the next value
any ideas?
y = Arrays.stream(subsets, i, i+6).sum()/6;
This Stream should give you all the y values:
IntStream.range(0, subsets.length - 10)
.map(i -> Arrays.stream(subsets, 0, 6).sum() / 6);
You could also use IntStream.average() instead of calculating it yourself if a double is ok.
sample is not special, it's only i+0 so you don't have to isolate it. You can try:
for(int i = 0; i < subsets.length-10; i++) {
int y = 0;
for(int j=i;j<i+n-1;j++) {
y += (int)subsets[j];
}
y = y / n;
}
Your approach of an nested loop is correct. Try something like this:
for ( int i = 0; i < subsets.length - 10; i++ )
{
sample = subsets[ i ];
int y = sample;
for ( int k = i; k < i + 5; k++ )
{
y += subsets[ k ];
}
y /= 6;
}
int sample;
for (int i = 0; i < subsets.length - 10; i++) {
sample = (int) subsets[i];
for (int j = i; (j < i + 6) && (j < subsets.length -10); j++) {
sample += subsets[i];
}
int y = sample / 6;
}

ArrayList<int[]> displaying last array only

I'm trying to write a "Mastermind" AI program and at the moment I'm trying to implement a naive AI which searches all possible 1296 combination of 4 pegs with 6 colours.
I have written up the following for loop to print out all combinations:
int compguess[] = new int[4];
int a, b, c, d;
ArrayList<int[]> combination = new ArrayList<int[]>();
for (int z = 0; z < 6; z++) {
for (int x = 0; x < 6; x++) {
for (int i = 0; i < 6; i++) {
for (int k = 0; k < 6; k++) {
a = z;
b = x;
c = i;
d = k;
compguess[0] = a;
compguess[1] = b;
compguess[2] = c;
compguess[3] = d;
combination.add(compguess);
When I run this code with System.out.println("combination" + Arrays.toString(combination.get(k))); at the end. This displays the combinations properly, however when I try to do the following:
for(int i=0; i< height; i++){
int[] temp = combination.get(i);
for(int j = 0; j < 4 ; j++){
state[i][j] = temp[j];
}
guess.addActionListener(this);
}
It only displays the last element (4,4,4,4) 40 times, instead I want it to be (0,0,0,0), (0,0,0,1), (0,0,0,2), (0,0,0,3), (0,0,0,4), (0,0,0,5), (0,0,1,0), (0,0,1,1), (0,0,1,2), (0,0,1,3) only 10 which is the size of the height
The problem is that you are using the same array every time, causing a change to one of them to change all, as they are, in fact, the same. Just reinitialize the array in the innermost for loop:
for (int z = 0; z < 6; z++) {
for (int x = 0; x < 6; x++) {
for (int i = 0; i < 6; i++) {
for (int k = 0; k < 6; k++) {
compguess = new int[4];
// rest of your code

how to cut a 3X3 array from a given grid with out indexOutOfBound exception

im stuck building a function that takes a given 2d-char array and a Point inside of it, that takes the given point coordinate and builds a new 2d-char array that surrounds the point.
for example if the original grid is:
a b c d e
z h a f c
g y z q x
r z x s a
k j h z z
the answer of point (0,0) should be:
? ? ?
? a b
? z h
the answer of point(2,2) should be:
h a f
y z q
z x s
public int numOfColors(Point p) {
char [][] neighbors = new char[3][3];
for(int i = 0; i < neighbors.length; i++){
for(int j = 0; j < neighbors.length; j++){
neighbors[i][j]='?';
}
}
int pX = p.getX(), pY = p.getY();
for(int i = pX - 1; (i < map.length) && (i < pX + 1) ; i++){ //runs on the original map and copy to neighbors
for (int j = pY - 1; (j < map.length) && (j < pY + 1); j++){
neighbors[i-pX+1][j-pY+1]=map[i][j];
}
}
for(int k = 0; k < neighbors.length; k++){ //runs on the neighbors array. if a point is an edge point.copy the given point color to the '?' cell
for(int l = 0; l < neighbors.length; l++){
if(neighbors[k][l]=='?')
neighbors[k][l]= map[pX][pY];
}
}
I've been sitting on this all day, the correct answer will be rewarded with a beer
I would like to begin from a helper method
private char getValue(int x, int y) {
if (x < 0 || x >= xSize || y < 0 || y >= ySize) {
return '?';
}
return neighbors[x][y];
}
public static void extract(char [][] arr, int row, int col){
char [][] ex = new char [3][3];
int rc = 0;
int cc = 0;
for(int i=row-1; i<row+2; i++){
for(int j=col-1; j<col+2; j++){
if(i < 0 || j < 0 || i >= arr.length || j >= arr[i].length){
ex[rc][cc] = '?';
} else {
ex[rc][cc] = arr[i][j];
}
cc++;
}
rc++;
cc = 0;
}
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
System.out.print (ex[i][j] +" ");
}
System.out.println();
}
}
Row, Col is Point(x,y).
I'm assuming the out of bounds exception is happened when using this bit of code, since you aren't checking if px - 1 is -1 and the same for py:
for(int i = px - 1; (i < map.length) && (i < px + 1) ; i++){ //runs on the original map and copy to neighbors
for (int j = py - 1; (j < map.length) && (j < py + 1); j++){
neighbors[i-pX+1][j-pY+1]=map[i][j];
}
}
You could change it to something like this:
int x1 = Math.max(p.getX() - 1, 0);
int x2 = Math.min(p.getX() + 1, map.length);
int y1 = Math.max(p.getY() - 1, 0);
int y2 = Math.min(p.getY() + 1, map[0].length);
int xCount = 0;
int yCount = 0;
and then:
for(int i = x1; i < x2; i++) { //runs on the original map and copy to neighbors
for (int j = y1; j < y2; j++) {
neighbors[xCount][yCount++]=map[i][j];
}
yCount = 0;
xCount++;
}

ArrayIndexOutOfBoundsException Java Issue

So I have been working on this problem for a while now. I keep getting an ArrayIndexOutOfBoundsException but I am unable to locate where the issue lies. If someone could point me in the right direction, I would really appreciate it! Thanks!
public class Answer {
public static void main(String[] args){
double[] y = {23, 11.1, 50.4};
double[] x = {22.2, 46, 100.0};
Answer answer = new Answer();
answer.answer(y, x);
}
public static int answer(double[] y, double[] x) {
int result = 0;
double percent_1, percent_2;
double[] compareList_1 = new double[x.length];
double[] compareList_2 = new double[y.length];
// Calculate percent of first 2 x value array items with y
// all y values. Store the results in a seperate list.
for(int i = 0; i < x.length; i++){
percent_1 = compare(y[i], x[0]);
percent_2 = compare(y[i], x[1]);
compareList_1[i] = percent_1;
compareList_2[i] = percent_2;
}
// Compare those lists to find common number
// There you have your answer.
result = (int)compareLists(compareList_1, compareList_2);
return result;
}
// Calculates percentage from x and y values
public static double compare(double y, double x){
double result = 1 - (y/x);
return result;
}
// Finds common value in lists
public static double compareLists(double[] list_1, double[] list_2){
for(int i = 0; i < list_1.length + 1; i++){
for(int j = 0; j < list_2.length + 1; j++){
if(list_1[i] == list_2[j]){
return list_1[i];
}
}
}
// Just cus this shouldn't ever return.
return 100;
}
}
In your iteration (compareLists), you should use 'length' (not length + 1)
for(int i = 0; i < list_1.length; i++)
for(int j = 0; j < list_2.length; i++)
I think the problerm is in
for(int i = 0; i < list_1.length + 1; i++){
for(int j = 0; j < list_2.length + 1; j++){
i < list_1.length + 1 or j < list_2.length + 1 change it to
for(int i = 0; i < list_1.length; i++){
for(int j = 0; j < list_2.length ; j++){
remove +1 from each condition.For j < list_2.length + 1 the list_2.length will give you length of array ie lastIndex +1 and you are adding another +1 in it causing loop condition to be j<lastIndex +1 giving you index error on the last iteration of loop in the line if(list_1[i] == list_2[j]){ for list_2[j]
Also in answer method you declare array by
double[] compareList_1 = new double[x.length];
double[] compareList_2 = new double[y.length];
and in the loop you are iterating upto x.length if x.length is greater than y.length the you can get the Index error in compareList_2[i] = percent_2;(inside the loop) because its length is y.length.

Divide and conquer matrix multiplication base case + how to split matrix into 4 quarters

I've been trying to code the divide and conquer matrix multiplication algorithm
There's problem that when I try to divide matrix into four quarters it gives me an error ArrayOutOfIndexBound
I'm not sure if I'm right about the base case, so can you help me out guys?
The problem that I get is at double[][] a21
public static double[][] divideAndConquer(double[][] a , double[][] b, int dimension){
if (a.length == 1){
double[][] result = new double[1][1];
result[0][0]= a[0][0]*b[0][0];
return result;
}
else {
int m = dimension/2;
double[][] a11 = new double[m][m];
for(int i = 0; i < m ; i++){
for (int j = 0 ; j< m ; j++)
a11[i][j]= a[i][j];
}
double[][] a21 = new double[m][m];
for(int i = m; i < dimension; i++){
for (int j = 0 ; j< m ; j++)
a21[i][j]= a[i][j];
}
double[][] a12 = new double[m][m];
for(int i = 0; i < m ; i++){
for (int j = m ; j< dimension ; j++)
a12[i][j]= a[i][j];
}
double[][] a22 = new double[m][m];
for(int i = m; i < dimension; i++){
for (int j = m; j < dimension; j++)
a21[i][j]= a[i][j];
}
double[][] b11 = new double[m][m];
for(int i = 0; i < m ; i++){
for (int j = 0 ; j< m ; j++)
b11[i][j]= b[i][j];
}
double[][] b12 = new double[m][m];
for(int i = 0; i < m ; i++){
for (int j = m ; j< dimension ; j++)
b12[i][j]= b[i][j];
}
double[][] b21 = new double[m][m];
for(int i = m; i < dimension; i++){
for (int j = 0 ; j< m ; j++)
b21[i][j]= b[i][j];
}
double[][] b22 = new double[m][m];
for(int i = m; i < dimension; i++){
for (int j = m; j < dimension; j++)
b21[i][j]= b[i][j];
}
double[][] x1 = divideAndConquer(a11,b11,m);
double[][] x2 = divideAndConquer(a12,b21,m);
double[][] x3 = divideAndConquer(a11,b12,m);
double[][] x4 = divideAndConquer(a12,b22,m);
double[][] x5 = divideAndConquer(a21,b11,m);
double[][] x6 = divideAndConquer(a22,b21,m);
double[][] x7 = divideAndConquer(a21,b12,m);
double[][] x8 = divideAndConquer(a22,b22,m);
..........................etc
As written, your problem is that you need to subtract off the array offset; e.g.,
a12[i][j]= a[i][j];
should be
a12[i][j-dimension]= a[i][j];
Your bigger problem is that you're creating 4 new submatrices, which is going to generate tons of garbage. Once you've gotten this working, I would strongly think about ways of doing this "in-place" by manipulating array indexes.
E.g., your new api would look like
public static double[][] divideAndConquer(double[][] a , double[][] b, int aMinIndex, int aMaxIndex, int bMinIndex, bMaxIndex){
and your divide and conquer would build subsets of the min & max indices.
How about using this for the partitioning? Please consider that it's in C and N/2 is the block size of each partition
for (int i = 0; i < N / 2; i++) {
for (int j = 0; j < N / 2; j++) {
ha11[i][j] = hA[i][j]; // top left
ha12[i][j] = hA[i][j + N / 2]; // top right
ha21[i][j] = hA[i + N / 2][j]; // bottom left
ha22[i][j] = hA[i + N / 2][j + N / 2]; // bottom right
hb11[i][j] = hB[i][j]; // top left
hb12[i][j] = hB[i][j + N / 2]; // top right
hb21[i][j] = hB[i + N / 2][j]; // bottom left
hb22[i][j] = hB[i + N / 2][j + N / 2]; // bottom right
}
}

Categories