如何使用Mathnet.numerics库创建跳过行和列的子矩阵? [英] How to create a submatrix skipping a row and a column using Mathnet.numerics library?

查看:112
本文介绍了如何使用Mathnet.numerics库创建跳过行和列的子矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编码以获得矩阵中不同元素的次要元素.我正在使用Mathnet.numerics库.我看到该库具有子矩阵方法,我需要在其中输入rowindex和rowcount.但是对于我来说,我需要通过跳过行和列来创建子矩阵(例如,对于3x3矩阵,对于元素(1,2),我需要跳过第一行和第二列来创建子矩阵).知道如何使用Mathnet.numerics的现有功能吗?

I am trying to code to obtain minors of different elements in a matrix. I am using Mathnet.numerics library. I see the library has submatrix method where I need to input rowindex and rowcount. But for my case I need to create submatrix by skipping rows and columns (for example, for a 3x3 matrix, for element (1,2), I need to skip the first row and second column to create my submatrix). Any idea how to use the existing functionality of the Mathnet.numerics?

推荐答案

c#
private double[,] getSubMatrix(double[,] _a, int i_start, int i_end, int j_start, int j_end)
    {
        double[,] _nex = new double[i_end - i_start + 1, j_end - j_start + 1];
        for(int i = i_start, i_sub = 0; i <= i_end; i++, i_sub++)
        {
            for(int j = j_start, j_sub = 0; j <= j_end; j++, j_sub++)
            {
                _nex[i_sub, j_sub] = _a[i, j];
            }
        }
        return _nex;
    }

示例:

c#
double[,] _a = { {22, 15, 1 },
             {42, 5, 38 },
             {28, 9, 4} };
getSubMatrix(_a, 1, 2, 1, 2);
//If u use MathNet
Console.WriteLine(DenseMatrix.OfArray(getSubMatrix(_a, 1, 2, 1, 2)).ToString());

您将获得: {5,38}, {9,4} 矩阵

U will get: {5,38}, {9,4} matrix

这篇关于如何使用Mathnet.numerics库创建跳过行和列的子矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆