Matrices: Introduction
Let’s understand the Matrices pattern, its real-world applications, and some problems we can solve with it.
We'll cover the following
Overview#
A matrix is a group of numbers arranged in rows and columns in a rectangular pattern. In computer science, matrixes are represented by 2D arrays with dimensions
Most of the problems requiring matrixes can be solved using the following operations:
Matrix transformations: A few transformations are scaling, translation, rotation, and reflection.
Matrix traversals: A few matrix traversals are simple, spiral, and diagonal traversals, as illustrated below.
We use matrixes when we need to:
Process the digital images. An image can be represented as a matrix, where each element of the matrix represents a pixel of the image. We can modify the images by applying different matrix transformations. For example, we can change color, brightness, or orientation of the images.
Represent a graph as an adjacency matrix where each row represents a vertex and each column represents an edge.
Store results of the subproblems calculated while solving a problem using dynamic programming.
Solve different mathematical equations—for example, various problems related to linear algebra.
Implement a grid game, such as chess, Candy Crush, Sudoku, Snake, or Tic-Tac-Toe.
Examples#
The following examples illustrate some problems that can be solved with these approaches:
1 of 2
2 of 2
Does my problem match this pattern?#
Yes, if the following condition is fulfilled:
The input data is given as a 2D array.
No, if the following conditions are fulfilled:
The input data is not given as a 2D array.
The input data is given as a 2D array, but it falls under some other pattern and can’t be solved by matrix operations. For example, graphs, dynamic programming, etc.
Real-world problems#
Many problems in the real world share the matrix transformation pattern. Let’s look at some examples:
In the video game industry, where matrixes play a major role in the manipulation of animations.
In Google's page ranking algorithms, where it is difficult to maintain a Google matrix representing links between web pages.
In cryptography algorithms, such as a Hill cipher, which uses matrix multiplication to encrypt and decrypt messages.
Strategy time!#
Match the problems that can be solved using the matrix transformation pattern.
Note: Select a problem in the left-hand column by clicking it, and then click one of the two options in the right-hand column.
Rotate an image.
Matrices
Find the longest palindromic sequence in a given string.
Some other pattern
Implement a basic calculator.
Given an map of a server center, return the number of servers that communicate with any other server.
Solution: Basic Calculator
Set Matrix Zeros