Home>

### java - matrix computation using two-dimensional arrays

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  ;// array to put the calculation result
for (int i = 0;i&lt;3;i ++) {
for (int j = 0;j&lt;1;j ++) {
for (int k = 0;k&lt;3;k ++) {
A [i] [j] + = (R [i] [k] * x [k] [j])-t [k] [j];
↑/↑ Is this right?
}
}
}``````

Multiple and subtract at the same time in the`for`statement that loops over the variable`k`.
Only multiplicationis calculated in the`for`statement that loops with the variable`k`.

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 more`for`statement added->total triple`for`statement)
Therefore, when multiplying, it is a triple`for`statement.

Let's calculate the subtraction part in the outer`for`statement (in this case, variable`j`).
(Duplicate`for`statement)

``````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  ;
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