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 947ed2f

Browse files
committed
add matrix sum operator
1 parent ac20be0 commit 947ed2f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed
File renamed without changes.
File renamed without changes.

‎Assets/Scripts/Matrix.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public override string ToString()
2323
matrix += "[";
2424
for (int j = 0; j < cols; j++)
2525
{
26-
matrix += values[i * cols + j];
26+
matrix += values[i * cols + j];
2727

28-
if(j != cols-1)
28+
if(j != cols-1)
2929
{
3030
matrix += ",";
3131
}
@@ -35,4 +35,23 @@ public override string ToString()
3535

3636
return matrix;
3737
}
38+
39+
static public Matrix operator +(Matrix a, Matrix b)
40+
{
41+
if (a.cols == b.cols && a.rows == b.rows)
42+
{
43+
float[] v = new float[a.rows * a.cols];
44+
45+
for (int i = 0; i < v.Length; i++)
46+
{
47+
v[i] = a.values[i] + b.values[i];
48+
}
49+
50+
return new Matrix(a.rows, a.cols, v);
51+
}
52+
else
53+
{
54+
throw new Exception("The two matrixes must be the same length!");
55+
}
56+
}
3857
}

0 commit comments

Comments
(0)

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