2-Matrix
Let’s get back to our chessboard game, in the past page we just use a single part of the chessboard to illustrate how a vector in real life could be, now we use the chessboard to understand how a matrix can be implemented.
positioned in the row x and column y, however we can also use alternative name for the respective row and columns but we ll see it in a while.
Matrix is a data structure composed of a number of rows and columns, where every single element has it's own coordinates to be read/set and also each single value can be a type as we've already specified during the vector declaration.
In the chessboard in picture 3 we can say that the 7-value is in position (3,2), 3 columns and 2 rows but we can also specifies names to identify the rows and colums of this data structure, parameters used is called dimnames() and we ll use it in a while.
Before writing the matrix definition you should take a look at the following picture, it is how a matrix is defined in R where “Element in position x.y” is the analog of “Pawn positioned in row x and column y”.
Example of a 2x2
Chessboard positions
Now the question is how to insert chessboard pawns in the R matrix? By row or by column? Using a single or multiple vectors? We define a matrix using a single vector and indicating how many rows and columns our matrix will be composed of, so the question is just to convert our chessboard into a vector that will be used by R to realize the matrix.
The method I use for putting a chessboard into a vector is to allign ever chessboard row one by one beginning from the position 1.1, is like the following picture and has the same structure used by R.
every single Element can be read and/or set using the respective Row and Colum numbers, let's now finally define a matrix using R.
Def
A matrix is defined in the following way
Matrix
(
c("Element in Position 1,1", "Element in Position 1,2", "Element in Position 2,1" ...,"Element in Position n,n"),
byrow=TRUE/FALSE,
ncol="n° columns",
nrow="n° rows"
dimnames=list(c("1° Row Name","2° Row Name",.....etc),c("1° ColName","2° Col Name","3° Col Name"....etc))
)
The first line is very simple to understand but we spent a bit of time trying to explain better the single attributes, let's start with "byrow", it gives the "direction" of the single elements we ve specified in the first line
In the following picture you can see the "direction" of creating a matrix using byrow=TRUE or not, i've used some arrows to let u understand better how it works, we start taking values from the “Start” and we go on using the direction indicated by the gray arrow but take attention that when a row/column is finished we start from the beginning of the following row/column until we reach the “End” of the chessboard.
Riassuming byrow=TRUE
Byrow=False
After byrow we can define how many rows and columns the matrix will be composed of, I don’t want to spend time on this definition is important to notice that if we use bigger dimensions of the vector R will get back to “Start” to put every single value till the matrix will be full.
Finally we have dimnames(), this attribute allow us to change rown and coloumn names with the choice you prefer, we can use text labels instead of the classic number notation, R use numbers by default.
Try check the past pictures, you can see that I’ve written different labels for rows and columns in this way we can get values indicating not a combination of numbers but a combination of text.
I have to admit that I prefer number notation instead of text notation but It’s just my opinion and my personal habbit.
Matrix Selections
-Selection of a single/multiple values in one or more positions is very simple, all we have to do is write the positions in square brackets right after the Matrix Name, first indicating the Row/Rows and then the Column/Columns .
"Matrix Name"["Row/Rows where the Element n is",”Coloumn/Columns where the Element n is”]
Before we start with selections it’s important to know that now we are in a multidimensional array and not in a “Single Dimension vector”, it means that from a matrix we can extract a vector or even a submatrix.
Remember that as I’ve told u before we can use numbers or text as rows and columns names, so the examples I write are a mix of these kind of stuff but before proceding let’s define the two matrix:
Numbered:
matrix<-matrix(c(7,16,9,16,9,11,2,5,1),
byrow=TRUE,
ncol=3,
nrow=3)
Text :
matrix<-matrix(c(7,16,9,16,9,11,2,5,1),
byrow=TRUE,
ncol=3,
nrow=3,
dimnames=list(c("Sole","Pizza","Amore"),c("Qui","Quo","Qua")))
Example:
Example:












Nessun commento:
Posta un commento