- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试将此函数从 Jagged Array 转换为 2D array,但我无法转换所有内容原始函数:
public static double[][] InvertMatrix(double[][] A)
{
int n = A.Length;
//e will represent each column in the identity matrix
double[] e;
//x will hold the inverse matrix to be returned
double[][] x = new double[n][];
for (int i = 0; i < n; i++)
{
x[i] = new double[A[i].Length];
}
/*
* solve will contain the vector solution for the LUP decomposition as we solve
* for each vector of x. We will combine the solutions into the double[][] array x.
* */
double[] solve;
//Get the LU matrix and P matrix (as an array)
Tuple<double[][], int[]> results = LUPDecomposition(A);
double[][] LU = results.Item1;
int[] P = results.Item2;
/*
* Solve AX = e for each column ei of the identity matrix using LUP decomposition
* */
for (int i = 0; i < n; i++)
{
e = new double[A[i].Length];
e[i] = 1;
solve = LUPSolve(LU, P, e);
for (int j = 0; j < solve.Length; j++)
{
x[j][i] = solve[j];
}
}
return x;
}
到目前为止我已经转换了什么:
public static double[,] InvertMatrix(double[,] A)
{
int n = A.Length;
//e will represent each column in the identity matrix
double[] e;
//x will hold the inverse matrix to be returned
double[,] x = new double[n][];
for (int i = 0; i < n; i++)
{
//how to convert this line?
x[i] = new double[A[i].Length];
}
/*
* solve will contain the vector solution for the LUP decomposition as we solve
* for each vector of x. We will combine the solutions into the double[][] array x.
* */
double[] solve;
//Get the LU matrix and P matrix (as an array)
Tuple<double[,], int[]> results = LUPDecomposition(A);
double[,] LU = results.Item1;
int[] P = results.Item2;
/*
* Solve AX = e for each column ei of the identity matrix using LUP decomposition
* */
for (int i = 0; i < n; i++)
{
//This one too?!
e = new double[A[i].Length];
e[i] = 1;
solve = LUPSolve(LU, P, e);
for (int j = 0; j < solve.Length; j++)
{
x[j,i] = solve[i,j];
}
}
return x;
}
如何将x[i] = new double[A[i].Length]转为二维数组?
最佳答案
这段代码很有用
static T[,] To2D<T>(T[][] source)
{
try
{
int FirstDim = source.Length;
int SecondDim = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular
var result = new T[FirstDim, SecondDim];
for (int i = 0; i < FirstDim; ++i)
for (int j = 0; j < SecondDim; ++j)
result[i, j] = source[i][j];
return result;
}
catch (InvalidOperationException)
{
throw new InvalidOperationException("The given jagged array is not rectangular.");
}
}
用法:
double[][] array = { new double[] { 52, 76, 65 }, new double[] { 98, 87, 93 }, new double[] { 43, 77, 62 }, new double[] { 72, 73, 74 } };
double[,] D2 = To2D(array);
UPD:对于那些可以接受不安全上下文的场景,有一个更快的解决方案,谢谢 Styp:https://stackoverflow.com/a/51450057/3909293
关于c# - 将锯齿状数组转换为二维数组 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26291609/
使用Cocos2d画一个更粗的圆: glLineWidth(20); ccDrawCircle(self.ripplePosition, _radius, 0, 50, NO); 但这就是显示的内容(
本学期我在计算机科学类(class)中遇到了一个挑战问题,这是上学期的复习题,但问题是:“给定一个参差不齐的数组,查找数组中是否有任何行的乘积为 48,如果是,则返回该行号。如果没有行包含 48 的乘
我一直在尝试将 sklearn 决策树中的 .dot 图插入到 pyplot 子图中,并且一直在努力做到这一点。 pygraphviz 库拒绝在我的 Windows 系统上工作,因此我使用以下方法插入
我有一个 UITableViewCell 子类,它有一些标签。所有这些标签都带有模糊或锯齿状的文本。它在设备上比在模拟器上更引人注目。 这是一个看起来很正常的标签: 这是一个看起来很糟糕的标签: 我该
作为LINQ-to-Entities投影的结果,我最终得到一个List,如果手动创建它,其外观如下所示: List data = new List(); data.Add(new ChartDataR
为什么我的css圈不流畅? 如果我做一个 HTML5 Canvas 真的很棒。 #circle { width: 100px; height: 100px;
我不明白 Numpy.arrays 相乘时会发生什么。 例如,带有锯齿状(或参差不齐)的数组 import numpy as np a = np.array([[1,2,3],[100,200]])
我正在尝试用 Python 计算时间序列的 Hurst 指数,该值决定了量化金融时间序列的一些均值回归特征。我采用了任意长度的时间序列,并选择将其拆分为数据 block ,该过程是计算 Hurst 指
我正在建立一个网站 - http://www.efficaxdevelopment.com 正如您在加载页面(在 IE 中)时看到的那样,页面上不是图像的文本或菜单看起来很糟糕,而在 FF 和 Chr
If you check, for instance, this shopping page ,您可以看到价格倾斜了几度。在 Chrome 上,这看起来“恰到好处”,在 Firefox 上,这看起来非
我是一名优秀的程序员,十分优秀!