Earn 20 XP


Learning Objectives

  • Vector
  • Matrix

Vector

  • A vector, in programming, is a type of array that is one-dimensional.

  • For example, [1, 2, 3, 4, 5] is a vector.

Here's a simple explanation of what it means for a vector to be one-dimensional:

Imagine you have a box with a bunch of toy cars inside. You can think of the box as an array, and each toy car as an element in the array. An array is like a list of things that are organized in a specific order.

Now, imagine that you only have one row of toy cars in your box. This means that the toy cars are all lined up in a single row, like this:

car 1, car 2, car 3, car 4, car 5

This is a one-dimensional array, or a vector, because there is only one row of toy cars. In other words, the array has only one dimension, or direction.

In contrast, if you had a box with multiple rows of toy cars, like this:

car 1, car 2, car 3 car 4, car 5, car 6 car 7, car 8, car 9

Then this would be a two-dimensional array, because it has two dimensions, or directions (up and down, as well as left and right).

Matrix

  • Matrix is an arrangement of numbers into rows and columns.

image.png

  • The matrix A has two rows and three columns.

With that let's understand Matrices with an intuitively example. Imagine you have a box with a bunch of toy cars inside, and you want to organize them into rows and columns. You can think of the box as a matrix, and each toy car as an element in the matrix. A matrix is like a table of numbers that are organized into rows and columns.

For example, you could have a matrix with three rows and three columns, like this:

car 1, car 2, car 3 car 4, car 5, car 6 car 7, car 8, car 9

In this matrix, the first row has three toy cars (car 1, car 2, and car 3), the second row has three toy cars (car 4, car 5, and car 6), and the third row has three toy cars (car 7, car 8, and car 9).

Each toy car has a specific position in the matrix, based on its row and column. For example, car 5 is in the second row and the second column of the matrix. You can use the row and column indices to identify the position of each toy car in the matrix.

Shape of Matrix

  • Since matrix A has two rows and three columns, we write its shape as , pronounced as ‘two by three’.

Dimension of Matrix

  • The shape of the matrix also tells you its dimensions. The dimension of matrix A is 2 as its shape is 2 3.
  • If a matrix has a shape, say, , the dimension of the matrix would be 3.
  • Don’t worry if you don’t get this point. It will become clearer as you go along.

Matrix

  • Matrix B has three rows and two columns, so it is a matrix

image.png

  • Whenever you are working with matrix shape, remember rows columns.

Both vectors and matrices are useful for organizing and storing data, and they can be used in different ways to solve problems. For example, you could use a vector to keep track of the scores in a game, or you could use a matrix to represent the positions of objects on a screen.