Matrix or Vector, huh?

Asked

Viewed 278 times

4

What are the advantages of using a matrix, since the vector is easier to use and does the same things?

2 answers

9

Their statement is not correct, they do not do the same thing, may even be similar in foundation but are different and definitely do not do the same thing.

In a very simple way we can say that vectors are one-dimensional (one-dimension) data structures that store data.

We can think of a vector like this representation:

inserir a descrição da imagem aqui

That is, at position 1 value 11, at position 2 is 15, and so on. In code it could be:

a[1] == 11

Already a matrix has more than one dimension, in general two, but can have more, ie the goal is to have a pair of information for example, what can not be done with a vector.
Here a re-presentation:

inserir a descrição da imagem aqui

That is, position 1.1 is 4, position 1.2 is 16, position 2.1 is 12, and so on, or code something like that:

b[1,1] == 4

With this you can see the difference between the structures and notice that one does not do what the other does, it is a matter of seeing which scenario apply each.

A classic example of programming classes is the one that asks "Enter with 4 notes and calculate the average". In this case, we need a vector, because it’s only one dimension: notes.

Now for the following case "Enter codes of 4 students, and for each student 4 grades" we have a two-dimensional scenario: student x notes, ie an example of matrix.

In short, the question is not advantage, they have different purposes and should be used for the case that need to be applied.

  • interesting what you said in the part "Now for the following case "Enter with codes of 4 students, and for each student 4 grades" we have a two-dimensional scenario: student x grades, ie an example of matrix."

  • This, for this case a simple vector would not solve, hence for each case can analyze and see which structure will work

8

It has no advantage if you don’t need the matrix. It is not a question of having one advantage over the other, these two types of data are not comparable, they are for different needs. So to say that you do the same things is wrong, and should provide an example by doing the same things.

The vector you’re saying is for a data sequence and almost always solves what you want. An array has more than one dimension, so at least it has rows and columns (can have more dimensions) and you use it when you need it, otherwise you don’t use it. In fact everything in computing is like this, you use what you need (when the person does not understand well what is doing he uses what he does not need).

I don’t even know if one way can be said to be easier than the other because they are not comparable, but it is to always use the simplest that solves your problem and never invent to use what you do not need.

To know which one to use, you have to define well what your problem is, what your needs are, what’s the hardest part of programming, because memorizing syntax and copying ready-made code is easy, making decisions is what makes the difference between who knows how to program and who doesn’t. And to make proper decisions you need to have a deep knowledge of everything you use, you can’t just use it superficially.

In commercial programming we don’t usually use "matrices", this is a concept much more used in scientific programming (of course a commercial application can have a small portion that is scientific). However you may find uses conceptually wrong. In commercial programming we usually have lines, ie lists, vectors. You may be thinking that there are things that have columns, and yes, it is true, but not these columns are of types, usually classes. Then you encapsulate the columns in a specific type and play in a array. Naive programmers would create a matrix if the types were homogenies or multiple arrays were they heterogeneous.

As you always have to analyze each case, you can’t give a general definition. This is one of the problems of artificial examples, it gives an idea that something should be done in a way and not always this is true.

In C#

Note that you use the term vector, but in C# you only use the term array, they end up being used as synonyms, although not exactly so. I understood that you want to know this, but know that there is a class called Vector.

What you’re calling the matrix actually in C# is called array two-dimensional. There is a class called Matrix, and has a structure Matrix3D and Matrix4x4. It would be good to read the documentation of these types to better understand their uses. I think the question does not refer to this and if this is true the term matrix should not be used in this context.

The array standard two-dimensional C# background is a array normal, that is, a sequence of data, because memory is always a sequence of data, it is linear, it does not have a rectangular shape. The access as if it were a matrix is given by a calculation. If it has two dimensions you have basically this formula:

índice da linha vezes o número de colunas total mais o número da coluna

or

lin x totalCol + col

Remembering that this works for rows and columns starting at 0 (careful because in mathematics it usually starts from 1 and this can fool you when it passes to the code, in C# and almost all languages to position 1,1 means second line and second column while in mathematics it means first line and first column, so it’s confusing to try to use the mathematical concept mixed with programming.

So to better visualize:

using static System.Console;

public class Program {
    public static void Main() {
        var array2D = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        WriteLine(array2D[0, 0]);
        WriteLine(array2D[2, 1]); //2 (linha) * 3 (total de colunas + 1 (coluna) = 7
        var array1D = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        WriteLine(array1D[0]);
        WriteLine(array1D[7]);
        for (int i = 0; i < array2D.GetLength(0); i++) {
            for (int j = 0; j < array2D.GetLength(1); j++) Write($"{array2D[i, j]} ");
            WriteLine();
        }
        for (int i = 0; i < array1D.Length; i++) Write($"{array1D[i]} ");   }
}

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

Note that in certain languages it would be possible to access as a array unidimensional directly in the "matrix", C# has an artificial protection not to let this occur, but in the background the 7 there would access the 2, 1 normally in array2D if it were allowed.

Understand that the List can’t be used for this, even though he has a array internally, you can’t create an array, you can create something similar, which is the list, which would be a array of array, which is different from a array Two-dimensional, since access cannot be done by doing this account I indicated above, and nothing guarantees that it will all be rectangular, it can have lines with different sizes, unless you control this in your algorithm. So there are other types like Matrix already cited, although it still makes no sense to compare with a List which is a type that allows you to change its size.

There is no clearly noticeable difference in performance in relation to one or the other (there is a simple arithmetic that matters almost nothing, the access time to memory is absurdly longer and this more calculation practically makes no difference, although it can measure something very small in large volumes).

  • then the vector and the matrix is used with the purpose of storing more things that a normal variable could get so I said they give in the same only I had doubts if there would be any difference as to performace or something related because I find matrix more difficult to use and already vector I find easy

  • @Adrianorufino I edited and talked about the performance.

Browser other questions tagged

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