Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit caddeec

Browse files
committed
add matrix product operation
1 parent 947ed2f commit caddeec

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

‎Assets/CreateMatrix.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ public class test : MonoBehaviour
88
void Start()
99
{
1010
float[] x = new float[6] { 1, 2, 3, 4, 5, 6 };
11+
float[] y = new float[6] { 1, 2, 3, 4, 5, 6 };
1112

1213
Matrix m = new Matrix(2, 3, x);
13-
Debug.Log(m.ToString());
14+
Matrix m2 = new Matrix(3, 2, y);
15+
16+
Debug.Log((m * m2).ToString());
1417
}
1518

1619
// Update is called once per frame

‎Assets/Scripts/Matrix.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,30 @@ public override string ToString()
5454
throw new Exception("The two matrixes must be the same length!");
5555
}
5656
}
57+
58+
static public Matrix operator *(Matrix a, Matrix b)
59+
{
60+
if (a.rows == b.cols)
61+
{
62+
float[] resultValues = new float[a.rows * b.cols];
63+
64+
for (int i = 0; i < a.rows; i++)
65+
{
66+
for (int j = 0; j < b.cols; j++)
67+
{
68+
for (int k = 0; k < a.cols; k++)
69+
{
70+
resultValues[i * b.cols + j] += a.values[i * a.cols + k] * b.values[k * b.cols + j];
71+
}
72+
}
73+
}
74+
75+
Matrix result = new Matrix(a.rows, b.cols, resultValues);
76+
return result;
77+
}
78+
else
79+
{
80+
throw new Exception("The number of rows of matrix A must match the number os columns of B!");
81+
}
82+
}
5783
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /