# Mac3D Matrix subtract from 2D

#### narvin

##### macrumors newbie
Original poster
I have the code bellow , which is for subtracting the 3D matrix from 2D.
Code:
``````import java.util.*;
class test28{

public static void main ( String[] args ) {

int [][][] arr1 = {{{6,3,9,0},{8,6,5,3},{5, 4, 6, 7}}};

int [][] arr2= {{6,3,5,0},{8,6,5,3}};

test28 test = new test28();

System.out.println(Arrays.deepToString(test.subtract(arr1,arr2)));

}
public static int [][] subtract(int[][][] a, int[][] b)  {
int[][] diff = new int[a.length][a.length];
for ( int j=0; j<a.length; j++){
for ( int k=0; k<a.length; k++){

diff[j][k] = a[j][k]- b[j][k];
}
}
return diff;
}
}
*************************************************************************************``````
Iam receiving error which is IndexOutOfBound

Last edited by a moderator:

#### chown33

##### Moderator
Staff member
"Two matrices must have an equal number of rows and columns to be added."​

Do your matrices meet this requirement?

Code:
``````int [][] arr2 = {
{6,3,5,0},  // row 0, has 4 columns
{8,6,5,3}  // row 1, has 4 columns
};  // 2 rows total, 4 columns total``````
If you think your code doesn't meet the requirement, then figure out what to change and where, so the matrices are subtractable.

• #### Amazing Iceman

##### macrumors 601
I think the problem is that you are using .length instead of .count in your for-loops, as in here:

for ( int j=0; j<a.length; j++){

Same may go to the other loop.
To be sure, use NSLog to output the value of j to the Console.

#### dylanryan

##### macrumors member
I think the problem is that you are using .length instead of .count in your for-loops, as in here:

for ( int j=0; j<a.length; j++){

Same may go to the other loop.
To be sure, use NSLog to output the value of j to the Console.
This is clearly Java code, not Objective C, so length is the correct way to see how large an array is.

That said, you are iterating over the wrong length. Looking at the code, here are some lengths that I would expect:

arr1.length => 1
arr1.length => 3
arr1.length => 4

arr2.length => 2
arr2.length => 4

Now, lets take this loop, and see what happens at the extreme cases

Code:
`````` for ( int j=0; j<a.length; j++){
for ( int k=0; k<a.length; k++){

diff[j][k] = a[j][k]- b[j][k];
}``````

So, j will go up to 2 at the largest (and k will go up to 3).
So, when j=2, then we try to access arr2[k], but arr2's length is 2, so the only valid indices are 0 and 1. There is no 2, hence the index out of bounds exception

Incidentally, I am not sure why arr1 is a 3D array. You only need 2 dimensions to store a matrix, no matter how many dimensions there are in the matrix. A 50-dimentional matrix fits comfortably in a 2D array.

#### Amazing Iceman

##### macrumors 601
This is clearly Java code, not Objective C, so length is the correct way to see how large an array is.

That said, you are iterating over the wrong length. Looking at the code, here are some lengths that I would expect:

arr1.length => 1
arr1.length => 3
arr1.length => 4

arr2.length => 2
arr2.length => 4

Now, lets take this loop, and see what happens at the extreme cases

Code:
`````` for ( int j=0; j<a.length; j++){
for ( int k=0; k<a.length; k++){

diff[j][k] = a[j][k]- b[j][k];
}``````

So, j will go up to 2 at the largest (and k will go up to 3).
So, when j=2, then we try to access arr2[k], but arr2's length is 2, so the only valid indices are 0 and 1. There is no 2, hence the index out of bounds exception

Incidentally, I am not sure why arr1 is a 3D array. You only need 2 dimensions to store a matrix, no matter how many dimensions there are in the matrix. A 50-dimentional matrix fits comfortably in a 2D array.
My bad, i wasn't paying too much attention. I thought it was Objective-C.

Register on MacRumors! This sidebar will go away, and you'll see fewer ads.