Home>

This is almost a duplicate of my previous question,
I'm stuck and I'll ask you a question again.

3x3 matrix R
And
3 × 1 matrix x, t
When there is
Rx-t
I want to calculate

I wrote a little bit of it, so here's what it looks like:
I'd like you to see if it's right (if you're not, please give me the correct answer).

I'm creating a program that does some calculations and puts the results into a file, but I'm looking for the cause because an impossible value comes out.

Code
float [] [] A = new float [3] [1];// array to put the calculation result
for (int i = 0;i<3;i ++) {
     for (int j = 0;j<1;j ++) {
          for (int k = 0;k<3;k ++) {
               A [i] [j] + = (R [i] [k] * x [k] [j])-t [k] [j];
↑/↑ Is this right?
               }
          }
     }
  • Answer # 1

    Multiple and subtract at the same time in theforstatement that loops over the variablek.
    Only multiplicationis calculated in theforstatement that loops with the variablek.

    In the first place, matrix addition and subtraction can be done by adding and subtracting components at the same place in the matrix.
    Multiplication is different. The components are multiplied, but the location is different.

    Therefore, when multiplying, it is a triple for statement.

    (M * n matrix with double + multiplication and one moreforstatement added->total tripleforstatement)
    Therefore, when multiplying, it is a tripleforstatement.

    Let's calculate the subtraction part in the outerforstatement (in this case, variablej).
    (Duplicateforstatement)

    public class Main {
        public static void main (String [] args) {
            int [] [] R = {{1,0, -1}, {2,3, -2}, {4,0,5}};
            int [] [] x = {{1}, {0}, {-1}};
            int [] [] t = {{-1}, {2}, {-3}};
            int [] [] answer = new int [3] [1];
            for (int i = 0;i<3;i ++) {
                for (int j = 0;j<1;j ++) {
                    for (int k = 0;k<3;k ++) {
                        answer [i] [j] + = R [i] [k] * x [k] [j];
                    }
                    answer [i] [j]-= t [i] [j];
                }
            }
            for (int i = 0;i<3;i ++) {
                for (int j = 0;j<1;j ++) {
                    System.out.print (answer [i] [j] + "");
                }
                System.out.println ();
            }
        }
    }


    <Calculation result>
    3
    2
    2 The element of

  • Answer # 2

    t is subtracted by the number of loops.
    In the loop, first focus on finding the matrix product, and then subtract after exiting the loop.

  • Answer # 3

    I haven't looked at the code carefully, but t [k] [j] is subtracted from A [i] [j] inside the three-stage loop, so t is Is it drawn three times?
    It is better to subtract t after calculating Rx.

  • Answer # 4

    If x, t is a 3 × 1 matrix, you can think of it as an array.