如何从矩阵中获取一维数组

发布于 2024-10-17 07:59:50 字数 1502 浏览 2 评论 0原文

我有一个矩阵。我需要从矩阵中获取一维数组。例如,我有以下矩阵:
123
456
789
所以它看起来像 3 个数组:147、258、369。 但我在这段代码中得到了“索引超出范围异常”:

  int[] b = new int[n];
        for (i = 0; i < n; i++)
        {
            b[i] = a[i, n];
            Console.Write(b[i] + " ");
        }
        Console.WriteLine();

感谢您的帮助。

这是已经可以运行的完整代码:

static void Main(string[] args)
    {        
        int n = 0, m = 0, i = 0, j = 0;           

        Random r = new Random();

        Console.WriteLine("Please, input matrix size:");
        Console.Write("\tn = ");

        n = Convert.ToInt32(Console.ReadLine());
        Console.Write("\tm = ");

        m = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine();

        int[,] a = new int[n, m];           

        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {                  
                a[i, j] = r.Next(0, 2);                   
            }                
        }

        showMe(a, n, m);

        Console.WriteLine();
        run(a, n, m);

        Console.ReadKey();

        int[][] b = new int[m][];
        for (i = 0; i < m; i++) 
        { 
            b[i] = new int[n]; 
            for (j = 0; j < n; j++) 
            {
                b[i][j] = a[j, i];
                Console.Write(b[i][j] + " ");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
        Console.ReadKey();
    }

I have a matrix. And I need to get 1D arrays from my matrix. For example, I have follow matrix:
123
456
789
So it looks like 3 arrays: 147, 258, 369.
But I got "Index out of range exception" in this code:

  int[] b = new int[n];
        for (i = 0; i < n; i++)
        {
            b[i] = a[i, n];
            Console.Write(b[i] + " ");
        }
        Console.WriteLine();

Thanx for any help.

Here's full code that works already:

static void Main(string[] args)
    {        
        int n = 0, m = 0, i = 0, j = 0;           

        Random r = new Random();

        Console.WriteLine("Please, input matrix size:");
        Console.Write("\tn = ");

        n = Convert.ToInt32(Console.ReadLine());
        Console.Write("\tm = ");

        m = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine();

        int[,] a = new int[n, m];           

        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {                  
                a[i, j] = r.Next(0, 2);                   
            }                
        }

        showMe(a, n, m);

        Console.WriteLine();
        run(a, n, m);

        Console.ReadKey();

        int[][] b = new int[m][];
        for (i = 0; i < m; i++) 
        { 
            b[i] = new int[n]; 
            for (j = 0; j < n; j++) 
            {
                b[i][j] = a[j, i];
                Console.Write(b[i][j] + " ");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
        Console.ReadKey();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终不够 2024-10-24 07:59:50

我假设 n=3 并且它是一个方阵,a[i, n]; 将超出 a 的范围 - 您可以引用的最大索引是 n-1。

我认为你想要的是

    int[][] b = new int[m][];
    for (i = 0; i < m; i++)
    {
        b[i] = new int[n];
        for (j = 0; j < n; j++)
        {
            b[i][j] = a[j,i];
        }

    }

b[0] 是你的第一个数组,b[1] 是你的第二个......

I'll assume n=3 and that it is a square matrix, a[i, n]; will be outside the bounds of a - the largest index you can reference is n-1.

I think what you want is

    int[][] b = new int[m][];
    for (i = 0; i < m; i++)
    {
        b[i] = new int[n];
        for (j = 0; j < n; j++)
        {
            b[i][j] = a[j,i];
        }

    }

b[0] is your first array, b[1] is your second....

空‖城人不在 2024-10-24 07:59:50

试试这个:

        int n = 3;
        int[,] a = new int[,] { 
            { 1, 2, 3 }, 
            { 4, 5, 6 }, 
            { 7, 8, 9 } };

        int[] b = new int[n];
        for (int i = 0; i < n; i++)
        {
            b[i] = a[i, n - 1];
            Console.Write(b[i] + " ");
        }
        Console.ReadLine();

这将输出 3 6 9,因为 n 没有改变。吉米是正确的,a[i,n] 需要是 a[i, n-1]。

Try this:

        int n = 3;
        int[,] a = new int[,] { 
            { 1, 2, 3 }, 
            { 4, 5, 6 }, 
            { 7, 8, 9 } };

        int[] b = new int[n];
        for (int i = 0; i < n; i++)
        {
            b[i] = a[i, n - 1];
            Console.Write(b[i] + " ");
        }
        Console.ReadLine();

This will output 3 6 9 since n is not being changed. Jimmy is correct, a[i,n] needs to be a[i, n-1].

我一向站在原地 2024-10-24 07:59:50

这通常适用于二维数组。

public static T[][] ToJaggedArray<T>(this T[,] arr)
{
    return Enumerable.Range(0, arr.GetUpperBound(0) + 1)
                     .Select(i => Enumerable.Range(0, arr.GetUpperBound(1) + 1)
                                            .Select(j => arr[i, j])
                                            .ToArray())
                     .ToArray();
}

public static T[][] ToJaggedArrayTranspose<T>(this T[,] arr)
{
    return Enumerable.Range(0, arr.GetUpperBound(1) + 1)
                     .Select(j => Enumerable.Range(0, arr.GetUpperBound(0) + 1)
                                            .Select(i => arr[i, j])
                                            .ToArray())
                     .ToArray();
}

// you'd be interested in ToJaggedArrayTranspose()
var mat = new[,]
{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
};
var arr = mat.ToJaggedArrayTranspose();
// arr === new[][] { new[] {1, 4, 7}, new[] {2, 5, 8}, new[] {3, 6, 9} }

ps,始终在多维数组上使用GetUpperBound()来获取维度的长度,不要尝试猜测它。

This works for two-dimensional arrays in general.

public static T[][] ToJaggedArray<T>(this T[,] arr)
{
    return Enumerable.Range(0, arr.GetUpperBound(0) + 1)
                     .Select(i => Enumerable.Range(0, arr.GetUpperBound(1) + 1)
                                            .Select(j => arr[i, j])
                                            .ToArray())
                     .ToArray();
}

public static T[][] ToJaggedArrayTranspose<T>(this T[,] arr)
{
    return Enumerable.Range(0, arr.GetUpperBound(1) + 1)
                     .Select(j => Enumerable.Range(0, arr.GetUpperBound(0) + 1)
                                            .Select(i => arr[i, j])
                                            .ToArray())
                     .ToArray();
}

// you'd be interested in ToJaggedArrayTranspose()
var mat = new[,]
{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
};
var arr = mat.ToJaggedArrayTranspose();
// arr === new[][] { new[] {1, 4, 7}, new[] {2, 5, 8}, new[] {3, 6, 9} }

p.s., Always use GetUpperBound() on multi-dimensional arrays to get the length of the dimensions, don't try to guess it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文