1

I'm currently trying to add a Matrix with an Array.

This is the code I have right now:

public void add(Matrix m) {

    for(int i = 0; i == values.length; i++) {
        for(int j = 0; j == values.length; j++) {
            m.values[i][j] = m.values[i][j] + values[i][j];
        }
    }

}

I would appreciate any help I can get, thanks!

8
  • 1
    i == values.length -> i < values.length. same for j Commented Oct 31, 2017 at 8:51
  • @Eran Already tried, I get this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Matrix.add(Matrix.java:23) at Matrix.main(Matrix.java:101) then. Commented Oct 31, 2017 at 8:53
  • Well, do m.values and values have the same dimensions? Do the number of rows equal the number of columns? You are assuming these things. Commented Oct 31, 2017 at 8:55
  • Matrix: Matrix m = new Matrix(new int[][] { { 2, 4, 5 }, { 3, 7, 2 }, { -2, 0, 1 }, { 5, 1, 1 } }); Commented Oct 31, 2017 at 8:57
  • 1
    try to use for (int i = 0; i < values.length; i++) { for (int j = 0; j < values[i].length; j++) { Commented Oct 31, 2017 at 9:02

3 Answers 3

2

In Java language there are no word of matrix or 2D or nD array there are an array of array.

About your problem try this :

for (int i = 0; i < values.length; i++) {
    for (int j = 0; j < values[i].length; j++) {
  • The first loop for (int i = 0; i < values.length; i++) { return array by array { 2, 4, 5 } and { 3, 7, 2 } and { -2, 0, 1 } and { 5, 1, 1 }
  • The second loop for (int j = 0; j < values[i].length; j++) { return value by values of each array from the first loop if we took the first array it will return 2 and 4 and 5
Sign up to request clarification or add additional context in comments.

8 Comments

I'm sorry I don't quite understand. Can you explain the second part in code?
@TimoGüntner the second part is equivalent to int[] array = values[i]; for (int j = 0; j < array.length; j++) { that's it you get it ;)
It's still not working for me. Can you help me once again and I'll mark you as the best answer.
The values of the Matrix M don't change. Can you give me an working example?
@TimoGüntner can you share the full code please there are many anonymity things in your code for example what is values and what is m
|
1

Check for the dimensions of the Array and the Matrix to be the same.

public void add(Matrix m) {
    for(int i = 0; i < values.length; i++) {
        for(int j = 0; j < values[i].length; j++) {
            m.values[i][j] = m.values[i][j] + values[i][j];
        }
    }
}

2 Comments

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Matrix.add(Matrix.java:23) at Matrix.main(Matrix.java:101)
Do the matrix and 2D array have the same dimensions? This error seems to imply that this might not be the case.
1

If you want to add matrices, you can use loops in Java, but also streams. This is a sample implementation of matrix addition using streams:

public class MatrixOperations {

    public static double[][] add(double[][] a, double[][] b) {
        return range(0, a.length).boxed().collect(
                () -> new double[a.length][a[0].length], // create the accumulator matrix which is to be returned
                (acc, row) -> range(0, a[row].length).forEach(col -> acc[row][col] = a[row][col] + b[row][col]), // sum each value
                (acc, r) -> {}); // ignore
    }

    // Test method
    public static void main(String[] args) {
        double[][] a = {{1.0, 3.0}, {1.0, 0.0}, {1.0, 2.0}};
        double[][] b = {{.0, .0}, {7.0, 5.0}, {2.0, 1.0}};
        double[][] sum = add(a, b);
        Stream.of(sum).map(Arrays::toString).forEach(System.out::println);
    }
}

If you run this class you will get the following output:

[1.0, 3.0]
[8.0, 5.0]
[3.0, 3.0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.