本文整理汇总了C#中Matrix.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.Add方法的具体用法?C# Matrix.Add怎么用?C# Matrix.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: compress
public Matrix compress(int k)
{
//Setting up compression matrix
int rows = matrix.RowCount;
int cols = matrix.ColumnCount;
Matrix VK = new Matrix(cols, k);
Matrix UK = new Matrix(rows, k);
double SK;
Matrix Ak = new Matrix(rows, cols);
//Begin compressing matrix according to algorithm
Console.WriteLine("Compressing matrix... K = " + k);
for (int i = 0; i < k; i++)
{
//Making V, U, and S for compressed matrix
VK = v.GetColumnVector(i).ToColumnMatrix();
UK = u.GetColumnVector(i).ToColumnMatrix();
SK = d[i, i];
//Performing operations on V, U, S
VK.Transpose();
UK = UK.Multiply(VK);
UK.Multiply(SK);
Ak.Add(UK);
}
//Returning data
SV1 = d[0, 0];
SVK1 = d[k + 1, k + 1];
Console.WriteLine("SV1: " + d[0, 0] + " SVK1: " + d[k + 1, k + 1]);
return Ak;
}
示例2: Main
static void Main()
{
Matrix<int> matrix1= new Matrix<int>(3,3);
matrix1.Add(1, 1, 3);
//Matrix<bool> boolMatrix = new Matrix<bool>(3, 3); //It can NOT be bool
//Matrix<StreamReader> stringMatrix = new Matrix<StreamReader>(3, 3);//It can NOT be string
Matrix<float> mat = new Matrix<float>(2, 2);
mat[0, 0] = 1;
mat[0, 1] = 3;
mat[1, 0] = 0;
mat[1, 1] = -2;
Matrix<float> mat2 = new Matrix<float>(2, 2);
mat2[0, 0] = 7;
mat2[0, 1] = 9;
mat2[1, 0] = 5;
mat2[1, 1] = 2;
Matrix<float> result = new Matrix<float>(2, 2);
result = mat * mat2;
Console.WriteLine(result.ToString());
if (mat2)
{
Console.WriteLine("The matrix is with non-zero elements");
}
else
{
Console.WriteLine("There is 0 in some of the cells");
}
}
示例3: AddExample
public void AddExample()
{
var matrix1 = new Matrix(2, 3);
matrix1[0, 0] = 1;
matrix1[0, 1] = 2;
matrix1[0, 2] = -4;
matrix1[1, 0] = 0;
matrix1[1, 1] = 3;
matrix1[1, 2] = -1;
var matrix2 = new Matrix(2, 3);
matrix2[0, 0] = -1;
matrix2[0, 1] = 0;
matrix2[0, 2] = 2;
matrix2[1, 0] = 1;
matrix2[1, 1] = -5;
matrix2[1, 2] = 3;
var expectedMatrix = new Matrix(2, 3);
expectedMatrix[0, 0] = 0;
expectedMatrix[0, 1] = 2;
expectedMatrix[0, 2] = -2;
expectedMatrix[1, 0] = 1;
expectedMatrix[1, 1] = -2;
expectedMatrix[1, 2] = 2;
var result = matrix1.Add(matrix2);
Assert.IsTrue(result.Equals(expectedMatrix));
}
示例4: Main
static void Main(string[] args)
{
//Creates Default_Matrix with given dimensions the matrix is empty
Matrix Default_Matrix = new Matrix(2, 2);
Console.WriteLine("Original Matrix:");
Console.WriteLine();
Default_Matrix.print();
Console.WriteLine();
double min; double max;
Point min_loc = new Point();
Point max_loc = new Point();
//Matrix to multiply,add,subtract, or test equals by.
double[,] Test_Matrix = { { 1, 2 }, { 1, 2 }};
Console.WriteLine("This is the Matrix after being multiplied by Test_Matrix:");
Console.WriteLine();
Default_Matrix.Mul(Test_Matrix);
Console.WriteLine();
Console.WriteLine("This is the Matrix after being multiplied by a scalar:");
Console.WriteLine();
Default_Matrix.scale(4);
Console.WriteLine();
Console.WriteLine("This is the Matrix that resulted from multiplying by the scalar and added to it is Test_Matrix:");
Console.WriteLine();
Default_Matrix.Add(Test_Matrix);
Console.WriteLine();
Console.WriteLine("This is the Matrix that resulted from multiplying by the scalar and then subtracting from it Test_Matrix:");
Console.WriteLine();
Default_Matrix.Sub(Test_Matrix);
Console.WriteLine();
Console.WriteLine("This is the Matrix transposed:");
Console.WriteLine();
Default_Matrix.Transpose();
Console.WriteLine();
Console.WriteLine("True or False: the matrices are equal?");
Console.WriteLine();
Default_Matrix.Equals(Test_Matrix);
Console.WriteLine();
Console.WriteLine("The current Matrix is reset to identity matrix:");
Console.WriteLine();
Default_Matrix.SetIdentity();
Console.WriteLine();
Default_Matrix.MinMax(out min, out max, out min_loc, out max_loc);
Console.WriteLine();
double j = Default_Matrix.Data;
Console.Write(j);
Default_Matrix.Data = 4;
Default_Matrix.print();
Console.Read();
}
示例5: Avg
public static Matrix Avg(ICollection<Matrix> set)
{
Matrix average = new Matrix();
average.Set(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f);
float hist = 0;
foreach (Matrix matrix3d in set) {
if (matrix3d.IsFloatValid()) {
average.Add(matrix3d);
hist++;
}
}
average.Mul(1f / hist);
return average;
}
示例6: ToString
/*
* Write a class Matrix, to holds a matrix of integers.
Overload the operators for adding, subtracting and multiplying of matrices, indexer for accessing the matrix content and ToString().
*/
static void Main(string[] args)
{
const int row = 5;
const int col = 5;
int value = 1;
Matrix sampleMatrix = new Matrix(row,col);
for (int rows = 0; rows < sampleMatrix.Row; rows++)
{
for (int cols = 0; cols < sampleMatrix.Col; cols++)
{
sampleMatrix.Add(value, rows, cols); ;
}
}
Console.WriteLine(sampleMatrix.ToString());
}
示例7: Solve
public long Solve()
{
var text = File.ReadAllLines(@"..\..\Problems\Problem082.txt");
var matrix = new Matrix();
var lineNumber = 0;
var columnNumber = 0;
foreach (var line in text)
{
foreach (var number in line.Split(','))
{
matrix.Add(lineNumber, columnNumber, int.Parse(number));
columnNumber++;
}
lineNumber++;
columnNumber = 0;
}
return matrix.MinPath();
}
示例8: NotImplementedException
public static Matrix operator -(Matrix matrix, Matrix other)
{
if (matrix.Row != other.Row && matrix.Col != other.Col)
{
throw new NotImplementedException("You're not allowed to add matrixes of different types!");
}
Matrix result = new Matrix(matrix.Row, matrix.Col);
int value = 0;
for (int row = 0; row < matrix.Row; row++)
{
for (int col = 0; col < matrix.Col; col++)
{
value = matrix[row, col] - other[row, col];
result.Add(value, row, col);
}
}
return result;
}
示例9: PlanarDLT
public static void PlanarDLT(Matrix cameraMatrix, Matrix distCoeffs, List<Matrix> worldPoints, List<System.Drawing.PointF> imagePoints, Matrix Rplane, Matrix Tplane, out Matrix R, out Matrix t)
{
// transform world points to plane
int n = worldPoints.Count;
var worldPlanePoints = new List<Matrix>();
for (int i = 0; i < n; i++)
{
var planePoint = new Matrix(3, 1);
planePoint.Mult(Rplane, worldPoints[i]);
planePoint.Add(Tplane);
worldPlanePoints.Add(planePoint);
}
var Rdlt = new Matrix(3, 3);
var Tdlt = new Matrix(3, 1);
PlanarDLT(cameraMatrix, distCoeffs, worldPlanePoints, imagePoints, out Rdlt, out Tdlt);
R = new Matrix(3, 3);
t = new Matrix(3, 1);
t.Mult(Rdlt, Tplane);
t.Add(Tdlt);
R.Mult(Rdlt, Rplane);
}
示例10: TestDLT
public static void TestDLT()
{
var cameraMatrix = Matrix.Identity(3, 3);
cameraMatrix[0, 0] = 700;
cameraMatrix[1, 1] = 700;
cameraMatrix[0, 2] = 250;
cameraMatrix[1, 2] = 220;
var distCoeffs = new Matrix(5, 1);
distCoeffs[0] = 0.05;
distCoeffs[1] = -0.1;
// generate a bunch of points in a volume
// project under some other camera (view)
var R = new Matrix(3, 3);
R.RotEuler2Matrix(0.2, 0.3, 0.3);
var t = new Matrix(3, 1);
t[0] = 2;
t[1] = 0;
t[2] = -4;
var modelPoints = new List<Matrix>();
var imagePoints = new List<System.Drawing.PointF>();
var zero3 = Matrix.Zero(3, 1);
for (float z = 1f; z <= 3.0f; z += 0.4f)
for (float y = -1f; y <= 1.0f; y += 0.4f)
for (float x = -1f; x <= 1.0f; x += 0.4f)
{
var model = new Matrix(3, 1);
model[0] = x;
model[1] = y;
model[2] = z;
modelPoints.Add(model);
// under our camera:
var transformedPoint = new Matrix(3, 1);
transformedPoint.Mult(R, model);
transformedPoint.Add(t);
var noise = Matrix.GaussianSample(zero3, 0.1 * 0.1);
transformedPoint.Add(noise);
double u, v;
Project(cameraMatrix, distCoeffs, transformedPoint[0], transformedPoint[1], transformedPoint[2], out u, out v);
var image = new System.Drawing.PointF();
image.X = (float)u;
image.Y = (float)v;
imagePoints.Add(image);
}
Console.WriteLine("x = [");
for (int i = 0; i < imagePoints.Count; i++)
Console.WriteLine("{0} {1}", imagePoints[i].X, imagePoints[i].Y);
Console.WriteLine("]';");
Console.WriteLine("X = [");
for (int i = 0; i < modelPoints.Count; i++)
Console.WriteLine("{0} {1} {2}", modelPoints[i][0], modelPoints[i][1], modelPoints[i][2]);
Console.WriteLine("]';");
Console.WriteLine("fc = [{0} {1}];", cameraMatrix[0, 0], cameraMatrix[1, 1]);
Console.WriteLine("cc = [{0} {1}];", cameraMatrix[0, 2], cameraMatrix[1, 2]);
Console.WriteLine("kc = [{0} {1} 0 0 0];", distCoeffs[0], distCoeffs[1]);
Console.WriteLine();
Console.WriteLine("R\n" + R);
Console.WriteLine("t\n" + t);
var Rest = new Matrix(3, 3);
var test = new Matrix(3, 1);
DLT(cameraMatrix, distCoeffs, modelPoints, imagePoints, out Rest, out test);
Console.WriteLine("Rest\n" + Rest);
Console.WriteLine("test\n" + test);
}
示例11: ToBig
public void ToBig ()
{
if (bigRect==smallRect) return;
upscaled = downscaled.Resize(bigRect, result:upscaled);
upscaled.Blur();
if (preserveDetail) upscaled.Add(difference);
isDownscaled = false;
}
示例12: CalibrateProjectorGroups
public void CalibrateProjectorGroups(string directory)
{
// for all cameras, take depth image points to color image points
var depthImage = new FloatImage(Kinect2Calibration.depthImageWidth, Kinect2Calibration.depthImageHeight);
var varianceImage = new FloatImage(Kinect2Calibration.depthImageWidth, Kinect2Calibration.depthImageHeight);
var validMask = new ByteImage(Kinect2Calibration.depthImageWidth, Kinect2Calibration.depthImageHeight);
foreach (var camera in cameras)
{
Console.WriteLine("projecting depth points to color camera " + camera.name);
// load depth image
string cameraDirectory = directory + "/camera" + camera.name;
depthImage.LoadFromFile(cameraDirectory + "/mean.bin");
varianceImage.LoadFromFile(cameraDirectory + "/variance.bin");
validMask.Zero();
var calibration = camera.calibration;
var depthFrameToCameraSpaceTable = calibration.ComputeDepthFrameToCameraSpaceTable();
// TODO: consider using just one 4x4 in calibration class
var colorCamera = new Matrix(4, 1);
camera.colorImagePoints = new List<Matrix>();
camera.depthCameraPoints = new List<Matrix>();
var depthCamera4 = new Matrix(4, 1);
// for each valid point in depth image
int numRejected = 0;
for (int y = 0; y < Kinect2Calibration.depthImageHeight; y += 1)
for (int x = 0; x < Kinect2Calibration.depthImageWidth; x += 1)
{
float depth = depthImage[x, y] / 1000f; // m
float variance = varianceImage[x, y];
if (depth == 0)
continue;
if (variance > 6 * 6)
{
numRejected++;
continue;
}
validMask[x, y] = (byte)255;
// convert to depth camera space
var point = depthFrameToCameraSpaceTable[y * Kinect2Calibration.depthImageWidth + x];
depthCamera4[0] = point.X * depth;
depthCamera4[1] = point.Y * depth;
depthCamera4[2] = depth;
depthCamera4[3] = 1;
// convert to color camera space
colorCamera.Mult(calibration.depthToColorTransform, depthCamera4);
//colorCamera.Scale(1.0 / colorCamera[3]);
// project to color image
double colorU, colorV;
CameraMath.Project(calibration.colorCameraMatrix, calibration.colorLensDistortion, colorCamera[0], colorCamera[1], colorCamera[2], out colorU, out colorV);
if ((colorU >= 0) && (colorU < (Kinect2Calibration.colorImageWidth - 1)) && (colorV >= 0) && (colorV < (Kinect2Calibration.colorImageHeight - 1))) // BEWARE: later do we round or truncate??
{
var colorImagePoint = new Matrix(2, 1);
colorImagePoint[0] = colorU;
colorImagePoint[1] = colorV;
camera.colorImagePoints.Add(colorImagePoint);
// expect a 3-vector?
var depthCamera = new Matrix(3, 1);
depthCamera[0] = depthCamera4[0];
depthCamera[1] = depthCamera4[1];
depthCamera[2] = depthCamera4[2];
camera.depthCameraPoints.Add(depthCamera);
//Console.WriteLine(depthCamera[0] + "\t" + depthCamera[1] + "\t -> " + colorImagePoint[0] + "\t" + colorImagePoint[1]);
}
}
SaveToTiff(imagingFactory, validMask, cameraDirectory + "/validMask.tiff");
Console.WriteLine("rejected " + 100 * (float)numRejected / (float)(Kinect2Calibration.depthImageWidth * Kinect2Calibration.depthImageHeight) + "% pixels for high variance");
}
// we never save colorImagePoints, depthCameraPoints, so we must remember to run previous
Console.WriteLine("elapsed time " + stopWatch.ElapsedMilliseconds);
// use decoded Gray code images to create calibration point sets
foreach (var projector in projectors)
{
string projectorDirectory = directory + "/projector" + projector.name;
projector.calibrationPointSets = new Dictionary<Camera, CalibrationPointSet>();
foreach (var camera in cameras)
{
string cameraDirectory = projectorDirectory + "/camera" + camera.name;
var decodedColumns = new ShortImage(Kinect2Calibration.colorImageWidth, Kinect2Calibration.colorImageHeight);
var decodedRows = new ShortImage(Kinect2Calibration.colorImageWidth, Kinect2Calibration.colorImageHeight);
//.........这里部分代码省略.........
示例13: MatrixTest
public static void MatrixTest()
{
//创建一个3*3的矩阵,并通过List赋值
Matrix m1 = new Matrix(3, 3, "m1");
IList<double> listsource = new List<double>() { 3, 2, 1, 4, 5, 6, 7, 8, 9, 11 };
m1.InsertDataFromList(listsource);
m1.DisplayMatrix(DataHelper.ConsoloOutput);
//创建一个3*3矩阵,然后通过默认方法赋值
Matrix m2 = new Matrix(3, 3, "m2");
m2.InsertMatrix(DataHelper.DefaultInput);
m2.DisplayMatrix(DataHelper.ConsoloOutput);
//m1+m2,然后赋值给m3
Matrix m3 = m1.Add(m2);
m3.DisplayMatrix(DataHelper.ConsoloOutput);
//m1*4
Matrix m4 = m2.DotMultiply(4);
m4.DisplayMatrix(DataHelper.ConsoloOutput);
//m5与m6相乘
Matrix m5 = new Matrix(2, 2, "m5");
m5.InsertDataFromList(new List<double>() { 1, 1, -1, -1 });
m5.DisplayMatrix(DataHelper.ConsoloOutput);
Matrix m6 = new Matrix(2, 2, "m6");
m6.InsertDataFromList(new List<double>() { -1, 2, 3, 0 });
m6.DisplayMatrix(DataHelper.ConsoloOutput);
Matrix m7 = m5.LeftMultipy(m6);
m7.DisplayMatrix(DataHelper.ConsoloOutput);
//找出m8中最大的值
Matrix m8 = new Matrix(4, 4, "m8");
m8.InsertDataFromList(new List<double>() { 1, 2, 4, 7, 6, 7, 8, 7, 9, 8, 7, 15, 4, 5, 8, 8, 9, 45, 5 });
m8.DisplayMatrix(DataHelper.ConsoloOutput);
int x8, y8;
double m8max = m8.GetMaxValue(out x8, out y8);
Console.WriteLine(string.Format("最大值坐标是第{0}行第{1}列,为{2}\n", x8 + 1, y8 + 1, m8max));
//求m8中所有元素之和
double sumRow1 = m8.GetSum(0, 0, 0, 3);
Console.WriteLine(string.Format("矩阵m8中第一行值之和为{0}", sumRow1));
double sumColumn1 = m8.GetSum(0, 0, 3, 0);
Console.WriteLine(string.Format("矩阵m8中第一列值之和为{0}", sumColumn1));
double sum = m8.GetSum();
Console.WriteLine("矩阵m8中所有值之和{0}\n", sum);
//求m8中元素的最大绝对值
int abma, abmb;
double maxAbs = m8.GetMaxAbsoluteValue(out abma, out abmb);
Console.WriteLine("m8的最大绝对值为第{1}行第{2}列的{0}\n", maxAbs, abma, abmb);
//获得m8的一个子矩阵(0,0)->(2,1)
Matrix m9 = m8.GetSubMatrix(0, 0, 2, 1);
m9.DisplayMatrix(DataHelper.ConsoloOutput);
m9 = m8.GetSubMatrix(0, 2, 3, 2);
m9.DisplayMatrix(DataHelper.ConsoloOutput);
//获得m8的归一化矩阵
//Matrix m10 = m8.GetNormalized();
//m10.DisplayMatrix(DataHelper.ConsoloOutput);
//获取m11的转置矩阵
Matrix m11 = new Matrix(4, 2, "m11");
m11.InsertDataFromList(new List<double>() { 1, 2, 4, 7, 6, 7, 8, 7, 9, 8, 7, 15, 4, 5, 8, 8, 9, 45, 5 });
m11.DisplayMatrix(DataHelper.ConsoloOutput);
Matrix m12 = m11.Transpose();
m12.DisplayMatrix(DataHelper.ConsoloOutput);
//获取m13的转置矩阵
Matrix m13 = new Matrix(5, 5, "m13");
//m13.InsertDataFromList(new List<double>() { 1, -3, 3, 3, -5, 3, 6, -5, 4 }); //结果为4,(1,1,2)
//m13.InsertDataFromList(new List<double>() { 2, 2, -2, 2, 5, -4, -2, -4, 5 });
//m13.InsertDataFromList(new List<double>() {4,2,-5,6,4,-9,5,3,-7 }); //结果为1,(1,1,1)
m13.InsertDataFromList(new List<double>
{
1,
0.5,
4,
3,
3,
2,
1,
7,
5,
5,
1/4.0,
1/7.0,
1,
1/2.0,
1/3.0,
1/3.0,
1/5.0,
2,
1,
1,
1/3.0,
1/5.0,
3,
1,
//.........这里部分代码省略.........
示例14: BuildWeightingMap
/// <summary>
/// Build the weighting maps
/// </summary>
private void BuildWeightingMap()
{
// All goal positions get a vaue of 10
staticCrateWeighting = new Matrix(goalMap, 10f);
// Corner goals get another 10
Bitmap cornerGoals = goalMap.BitwiseAND(cornerMap);
staticCrateWeighting = staticCrateWeighting.Add(new Matrix(cornerGoals, 10f));
// No crates will ever get onto a dead piece, but this will make the area around a dead cell less likely to be filled by crates
staticCrateWeighting = staticCrateWeighting.Add(new Matrix(deadMap, -2f));
staticCrateWeighting = staticCrateWeighting.Average();
// Player map
staticPlayerRating = new Matrix(controller.Map.Size);
// Leave the plaer map at zero for now
}
示例15: MatrixsSum_Test
public void MatrixsSum_Test(Matrix<int> firstMatrix, Matrix<int> secondMatrix, Matrix<int> resultMatrix ) {
SquareMatrix<int> result = firstMatrix.Add(secondMatrix);
Assert.AreEqual(resultMatrix.Equals(result), true);
}