1

I've a list made by sublists of numbers. This is named biglist and it is:

biglist[0] = { 1, 2, 3, 4, 5 };
biglist[1] = { 5, 3, 3, 2, 1 };
biglist[2] = { 3, 4, 4, 5, 2 };

Now I want to create a matrix using these sublists where each sublist represents a column of the matrix. My final result has to be a matrix 5x3 in this way:

1 | 5 | 3   
---------
2 | 3 | 4   
---------  
3 | 3 | 4   
---------  
4 | 2 | 5   
---------  
5 | 1 | 2  

I know how to convert a list to array but I don't know how to assemble these arrays to create the matrix.

I think the package Math.Net could work for my purpose, but I don't understand how it's possible to do this with it.

2 Answers 2

2

MathNet limitation is you can use only Double, Single, Complex or Complex32 numeric types for that purpose.

using MathNet.Numerics.LinearAlgebra;

// ...

double[][] biglist = new double[3][];

biglist[0] = new double[] { 1, 2, 3, 4, 5 };
biglist[1] = new double[] { 5, 3, 3, 2, 1 };
biglist[2] = new double[] { 3, 4, 4, 5, 2 };

Matrix<double> matrix = Matrix<double>.Build.DenseOfColumns(biglist);
Console.WriteLine(matrix);

Gives:

DenseMatrix 5x3-Double
1  5  3
2  3  4
3  3  4
4  2  5
5  1  2
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! What if I had lists of sublists? I mean If I had biglist made by biglist[0] and biglist[1] where biglist[0] is made up of biglist[0][0]=array1, biglist[0][1]=array2, biglist[1][0]=array3, biglist[1][1]=array4 and I would like to create two matrices, one with biglist[0] and the other with biglist[1]? Is there the chance to use for loop and Add with Math.Net?
@Roberto Ruggeri, you are welcome! Yes, it's possible. Btw, here, on stackoverflow we express our attitude by voting. E.g., I found your question interesting, so I upvoted it. If my answer seem helpful to you, you can upvote it too. If the answer solves your problem (and it appears to), you can accept it.
Hi @Alex sorry but I'm new here therefore I'm just learning everything :) I upvoted your answer too now! Could you please tell me how it is possible to do what I asked?
@Roberto Ruggeri, that's OK! In your case you can just have var matrix1 = Matrix<double>.Build.DenseOfColumns(biglist[0]) and var matrix2 = Matrix<double>.Build.DenseOfColumns(biglist[1]). Or you can populate a collection dynamically with for-loop and Add and then pass it to DenseOfColumns(...).
How can I populate a collection dynamically?? I don't want to create matrix1 and matrix 2 but only matrix which stores matrix[1] and matrix[2] as we do with Lists.
|
0

If I understand you very well you are trying to do something like this :

    public static int[,] GetMatrix(IReadOnlyList<int[]> bigList)
    {
        if (bigList.Count == 0) throw new ArgumentException("Value cannot be an empty collection.", nameof(bigList));

        var matrix = new int[bigList.Count, bigList[0].Length];

        for (var bigListIndex = 0; bigListIndex < bigList.Count; bigListIndex++)
        {
            int[] list = bigList[bigListIndex];

            for (var numberIndex = 0; numberIndex < list.Length; numberIndex++) matrix[bigListIndex, numberIndex] = list[numberIndex];
        }

        return matrix;
    }

    private static void Main(string[] args)
    {
        var biglist = new List<int[]>
        {
            new[] {1, 2, 3, 4, 5},
            new[] {5, 3, 3, 2, 1},
            new[] {3, 4, 4, 5, 2}
        };

        int[,] matrix = GetMatrix(biglist);

        for (var i = 0; i < matrix.GetLength(1); i++)
        {
            for (var j = 0; j < matrix.GetLength(0); j++)
                Console.Write($" {matrix[j, i]} ");
            Console.WriteLine();
        }


        Console.ReadKey();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.