Hello

I have a code here. it generates random 2d array kMean[2][3]
and then this program calculates the distance between random generated kMean[2][3]s and data[][].

but the problem is, when the random kMean is 1, then it calculates the distance, but when the number or kMean becomes more than one, it shows error.

i want that program to calculate all the distances between first kMean, and then calculate the second kMean as an array.

can anyone please help me to figure out the solution?

thank u :)

package testing_place;

import java.io.*;
import java.util.*;
/**
 *
 * @author ginger
 */
public class Clustering {

    static int[][] data = new int[][]{{5,1,1},
                                      {5,4,4},
                                      {3,1,1},
                                      {3,2,4},
                                      {5,1,1}};


    static int no_of_dataSet = data.length;

    final static int kNumber = 2;
    final static int kFeatureNum = 3;

    static Random randomInt = new Random();
    static int [][] kMean = new int [kNumber][kFeatureNum];

    static int [] distance = new int[no_of_dataSet*kNumber];//<-------------
    static int [] test = new int[no_of_dataSet*kNumber];



public static void kCentroid(){
        //fill the grid WITH RANDOM NUMBERS
	        for (int row = 0; row < kMean.length; row++) {
	           for (int col = 0; col < kMean[row].length; col++) {

	                kMean[row][col] = randomInt.nextInt(10);
	            }
	        }
        }


public static void calculateDist(){
    
            for(int j=0; j<kNumber; j++){
                for(int i=0; i<distance.length; i++){
        
distance[i]=(int) Math.sqrt((kMean[j][0]-data[i][0])*(kMean[j][0]-data[i][0])+
                            (kMean[j][1]-data[i][1])*(kMean[j][1]-data[i][1])+
                            (kMean[j][2]-data[i][2])*(kMean[j][2]-data[i][2]));


        }
    }


}

public static void message(){
	        //ONLY FOR PRINTING OUT
	        for(int i = 0; i < kMean.length; i++) {
                         System.out.print("k"+i+": ");

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

                                                System.out.println();

                for(int i = 0; i < data.length; i++) {
                         System.out.print("data"+i+": ");

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

 //======================================================
                 for(int i = 0; i < distance.length; i++) {
                  System.out.println(distance[i] + " <---");
                  
                 }

        }


public static void main(String[] args){

            kCentroid();
            calculateDist();
            message();


        }



}

when the random kMean is 1

kMean is an array. What does the above statement mean ?
In which line the error occurs ?

kMean is an array. What does the above statement mean ?
In which line the error occurs ?

on line 50
it can calculate the distance when kNumber equals 1, but it is more than 1, then it shows error on line 50 :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.