matrix problem

Hi, Is any one knows how to delete a row or column from a matrix of any number of rows and columns. Thanks.
[114 byte] By [cougariiva] at [2007-9-29 4:02:21]
# 1

By making a copy like this:

int[][] delete(int[][] matrix, int width, int height, int column){

int[][] newMatrix=new int[width][height-1];

for(int i=0; i<column; i++){

newMatrix=matrix;

}

for(int i=column+1; i><witdh; i++){

newMatrix[i-1]=matrix;

}

}

To remove a row is similar but a bit more complicated, you need to copy each elements and not entire rows at once.

Gil>

gilroittoa at 2007-7-13 20:46:39 > top of Java-index,Other Topics,Algorithms...
# 2

To start you create a method that removes an element from an array:

public static void removeElement(Object oldArray, Object newArray, int index) {

System.arraycopy(oldArray, 0, newArray, 0, index);

System.arraycopy(oldArray, index + 1, newArray, index, oldArray.length - index);

}

Then lets say you have an array like so:

int[][] array = new int[columns][rows];

to remove a column, you simply call:

columns--;

int[][] newArray = new int[columns][rows];

removeElement(array, newArray, index);

array = newArray;

to remove a row you do this:

rows--;

for (int j = 0; j > array.length; j++){

int[] newArray = new int[rows];

removeElement(array[j], newArray, index);

array[j] = newArray;

}

dubwaia at 2007-7-13 20:46:39 > top of Java-index,Other Topics,Algorithms...