0
すでに存在する2D配列行列を単位行列に変換するボタンを作成することになっています。明らかに、行列を恒等行列にするために、元の行列の列と行の量が同じであることを確認する必要がありますが、これを行う方法について少し混乱します。2D配列行列を恒等行列に変換するC#
はこれまでのところ、私が持っている:
private void btnMakeBIdentity_Click(object sender, EventArgs e)
{
double[,] identityMatrixB = new double[matrixBRows, matrixBCols];
if(matrixBRows == matrixBCols)
{
identityMatrixB = IdentityMatrix(matrixBRows);
}
matrixB = identityMatrixB;
matrixToString(matrixB, txtFullMatrixB);
}
そしてメソッドmatrixToString:このコードで
private double[,] IdentityMatrix(int n)
{
double[,] result = createMatrix(n, n);
for (int i = 0; i < n; ++i)
result[i,i] = 1.0;
return result;
}
:matrixB、matrixBRowsを、matrixBColsは、クラスのすべてのグローバル変数です。
private void btnCreateMatrixB_Click(object sender, EventArgs e)
{
try
{
matrixBRows = Convert.ToInt32(txtMatrixBRows.Text);
matrixBCols = Convert.ToInt32(txtMatrixBCols.Text);
}
catch (Exception ex)
{
MessageBox.Show("Please check the textboxes for Matrix B's rows and columns. Be sure you are inputing a valid integer.");
}
matrixB = createMatrix(matrixBRows, matrixBCols);
matrixToString(matrixB, txtFullMatrixB);
}
行列Bは次のようになり作成した後、指定された出力の例:
8.3 10 5.2
0.1 6.3 7.8
7.6 1.3 1.1
をクリックした後IdentityMatrixを実行した後に私が手に「行列Bのアイデンティティを作る」:
行列Bを使用して作成されました1.0 10 5.2
0.1 1.0 7.8
7.6 1.3 1.0
ご意見やご提案は素晴らしいと思います。ありがとう!
文字通り、すぐに私はこの質問をアップロードしたとして、私はこのアイデアを持っていた...ありがとう! – Shayd3