Transform matrix into vector

Asked

Viewed 991 times

-1

I am doing some exercises with matrix and array, and I arrived at the following situation:

    static void Main(string[] args)
    {
        int linha = 3;
        int coluna = 3;

        int[,] matriz = new int[linha, coluna];

        int count = 0;
        for (int l = 0; l < linha; l++)
        {
            for (int c = 0; c < coluna; c++)
            {
                matriz[l, c] = count;
                count++;
            }
        }

        var vetor = new int[matriz.Length];

        count = 0;
        for (int l = 0; l < linha; l++)
        {
            for (int c = 0; c < coluna; c++)
            {

                vetor[count] = matriz[l, c];
                count++;
            }
        }

        ReadKey();
    }

I generate this 3 x 3 matrix and then transform it into an array based on the size of the matrix.

  • Is there any way to improve this algorithm? Maybe using LINQ or other language resources to copy array items to the array.
  • The problem is not even the algorithm, it is its unnecessariness. Being an artificial algorithm makes it practically non-existent. If you have a rule that determines artificialism, what would it be? We don’t know what can be done to improve and what can’t just because it’s artificial. And what you’re looking at. For example, is organizing more code an item to be observed? Is standardizing code an improvement? Making code have fewer lines? And can it be less readable? If you can’t, what would be illegible? For me, for example, from my experience, the answer made it worse

  • @Good Maniero, my intention was to see other ways to do this, only that without having to use go inside for, maybe using Inuit or other resources of csharp to copy the items to the array, I will change the question to be clearer.

  • 1

    For Estou fazendo alguns exercícios com matriz e array I believe it is a case of exercising logic and in general asked to remove the loops.

2 answers

4

By "I’m doing some exercises with matrix and array" I understand it is a case of exercising logic and in general asked to remove the loops.

This could be done in a single for:

for (var i = 0; i < linha * coluna; i++)
{
    vetor[i] = matriz[i/coluna, i % coluna];
}

Full example (with slight modifications for easy reading):

using System;

public class Program
{
    public static void Main()
    {
        int linha = 3;
        int coluna = 4;

        var matriz = new string[linha, coluna];

        for (int l = 0; l < linha; l++)
        {
            for (int c = 0; c < coluna; c++)
            {
                matriz[l, c] = l.ToString() +"-"+ c.ToString();
            }
        }

        var vetor = new string[linha*coluna];

        for (var i = 0; i < linha * coluna; i++)
        {
            vetor[i] = matriz[i/coluna, i % coluna];
        }

        foreach (var elem in vetor)
            Console.WriteLine(elem);
    }
}

Link to fiddle: https://dotnetfiddle.net/lzz0Gc

2


I wasn’t going to answer that, but anyone who knows me knows there’s a case I can’t help. See comment because I find this kind of question complicated but already has an answer.

public class Program {
    public static void Main() {
        var linhas = 3;
        var colunas = 3;
        var matriz = new int[linhas, colunas];
        for (int i = 0, count = 0; i < linhas; i++) for (var j = 0; j < colunas; j++) matriz[i, j] = count++;
        var vetor = new int[matriz.Length];
        for (int i = 0, count = 0; i < linhas; i++) for (var j = 0; j < colunas; j++) vetor[count++] = matriz[i, j];
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

There is no major improvement that can be made, although improvement would need to be better defined. As I said in the commentary I would need parameter to know what can and cannot do, what would be considered improvement and what would not be. It is always possible to do it in a very different way, hence to call it better goes a distance.

I remade by writing a more streamlined, clean, short code, which I like best. Not everyone likes shorter code. The improvements are really cosmetic.

I standardized the code (every hour he chose to do it in a different way). I took what wasn’t necessary and narrowed the scope of some variables.

I gave the variables a better name: if something is a total it should be plural and in this type of manipulation the common is to use variables i and j, which even eliminates the l which is a bad name because always for the same 1.

What I didn’t do is include new things in the algorithm that I didn’t ask for and I didn’t try to narrow it down to a loop because it only makes the execution worse.

In addition to having a multiplication and two divisions that do not exist in the original, and these are the most expensive individual operations that a processor can perform, it has not yet reduced the amount of branches which is what could improve something in terms of performance.

Using LINQ might make it look like a better algorithm but it’s an illusion. would be slower and in some case could even be worse (I think it does not happen in this case, but there is a situation that the enumeration would happen exponentially). In the question it does not say anything about LINQ, only in the comment it suggests that this would be the possible improvement, that I question that it is better, would be more visibly simple, but not necessarily better. That’s why I wanted a better definition of the problem.

If you want to insist on LINQ you can make the transformation like this:

var vetor = matriz.OfType<int>().ToArray();

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

You’re exchanging a long line and another declaring array for a short, but the performance will be less, even if little.

If I were to improve further, I would eliminate almost all the code that only exists in this form artificially. I still don’t see the need to make the transformation, it may have, but it would need to be justified.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.