英文:
Matrix Multiplications: Vector to Matrix Conversion
问题
我刚刚开始学习Math.NET Numerics,并发现它在矩阵乘法方面非常有用和强大。对我来说现在有点挑战,因为我刚刚开始学习C#,我希望我能在这里找到一些解决方案,帮助我在这个学习过程中。提前谢谢!
目标:
-
从Excel文件中提取填充在第一和第二列的数据,并将它们放入一个nx2矩阵中(其中n可以是任意行数,在这里我设置为3000,所以是3000x2矩阵)。
-
从矩阵的第一列中提取数据,乘以5并存储在向量1(VColumn)中。类似地,从矩阵的第二列中提取数据,乘以2并存储在向量2(VRow)中。使用这种方法,我能够分别用不同的值乘以每一列。(是否有其他更直接的方法,可以使用".Multiply"关键字以矩阵形式分别乘以每一列的值?)从输出中看到的情况是显示为一行而不是一列。
-
将向量1和向量2放入矩阵形式(3000x2矩阵格式)。但我得到的输出是0。不确定这是否是正确的方法...我应该如何编写这部分代码或是否有其他替代方法?
我不确定这是否是编写矩阵乘法的正确方法。如果有其他替代方法,请分享,因为我还在学习。非常感谢!
英文:
I had just started to learn about Math.NET Numerics and find this useful and powerful for Matrix Multiplication purpose. It's quite challenging for me now as I had just started to learn C#, I hope I could find some solutions here to assist me throughout the journey. Thank you in advance!
Objectives:
-
Extract data from Excel file which is filled in the first & second column and put them in a nx2 Matrix (where n can be any number of rows, here I set 3000, so it's 3000x2 Matrix)
-
Extract data from the first column of the Matrix to multiply by 5 and store in a vector 1 (VColumn). Similarly, extract data from the second column of the Matrix to multiply by 2 and store in a vector 2 (VRow). With this method I'm able to multiply each column with different values. (Is there any other more direct methods to multiply each column value individually in Matrix form using .Multiply keywords?) From what I see from the output, it is display in a line instead of one column.
- To place vector 1 and vector 2 into Matrix form (3000x2 Matrix format). But the output I'm getting is 0. Not sure this is the correct way though... how do I code for this part or any other alternatives?
I'm not sure if this is the right way to code for Matrix Multiplications. If there are alternative ways, please do share as I'm still learning. Many thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.Data.Text;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Matrix<Double> fileMatrix = DelimitedReader.Read<Double>
(@"C:\Users\Student\Downloads\MatricesData.xls", false, "\t", true); //Objective 1
textBox1.AppendText(fileMatrix.ToString());
Vector<Double> x = Vector<Double>.Build.Dense(3000, 1); //Objective 2
Vector<Double> y = Vector<Double>.Build.Dense(3000, 1);
Vector<Double> VColumn = fileMatrix.Column(0);
Vector<Double> VRow = fileMatrix.Column(1);
VColumn.Multiply(5.0, x);
VRow.Multiply(1.0, y);
textBox1.AppendText(x.ToString());
textBox1.AppendText(y.ToString());
Matrix<Double> z = Matrix<Double>.Build.Dense(3000, 2); //Objective 3: Need help
z.InsertColumn(0, VColumn);
z.InsertColumn(1, VRow);
textBox1.AppendText(z.ToString());
}
}
}
答案1
得分: 1
使用线性代数,您可以通过矩阵*矩阵乘法来实现相同的结果。
使用MathNet
,上述代码如下:
static void Main(string[] args)
{
var fileMatrix = DelimitedReader.Read<double>("data.csv",
sparse: false,
delimiter: "\t",
hasHeaders: true
);
var coefMatrix = CreateMatrix.DiagonalOfDiagonalArray(new double[] { 5, 2 });
var resultMatrix = fileMatrix * coefMatrix;
}
<details>
<summary>英文:</summary>
Using linear algebra you can achieve the same result using matrix*matrix multiplication
[![pic][1]][1]
Using `MathNet` the above is
static void Main(string[] args)
{
var fileMatrix = DelimitedReader.Read<double>("data.csv",
sparse: false,
delimiter: "\t",
hasHeaders: true
);
// 5.2 1.8
// 3.2 0.2
// 1.8 2.8
// 4.4 3.4
// 5.2 0.6
// ...
var coefMatrix = CreateMatrix.DiagonalOfDiagonalArray(new double[] { 5, 2 });
var resultMatrix = fileMatrix * coefMatrix;
// 26 3.6
// 16 0.4
// 9 5.6
// 22 6.8
// 26 1.2
// 16 2.8
// ...
}
[1]: https://i.stack.imgur.com/eDmck.png
</details>
# 答案2
**得分**: 0
InsertColumn 创建一个新矩阵并插入一列:
**Matrix<T> InsertColumn(int columnIndex, Vector<T> column)**
创建一个新矩阵,并在指定索引处插入给定列。
你想要某个版本的 SetColumn:
**void SetColumn(int columnIndex, Vector<T> column)**
或
**void SetColumn(int columnIndex, int rowIndex, int length, Vector<T> column)**
或
**void SetColumn(int columnIndex, Double[] column)**
<details>
<summary>英文:</summary>
InsertColumn creates a new matrix and inserts one column:
**Matrix<T> InsertColumn(int columnIndex, Vector<T> column)**
Creates a new matrix and inserts the given column at the given index.
You want some version of SetColumn:
**void SetColumn(int columnIndex, Vector<T> column)**
or
**void SetColumn(int columnIndex, int rowIndex, int length, Vector<T> column)**
or
**void SetColumn(int columnIndex, Double[] column)**
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论