Manipulating matrices in Julia Last Updated : 22 Apr, 2020 Comments Improve Suggest changes Like Article Like Report Matrices in Julia are the heterogeneous type of containers and hence, they can hold elements of any data type. It is not mandatory to define the data type of a matrix before assigning the elements to the matrix. Julia automatically decides the data type of the matrix by analyzing the values assigned to it. Because of the ordered nature of a matrix, it makes it easier to perform operations on its values based on their index. Following are some common matrix manipulation operations in Julia: Transpose of a matrix Flipping a matrix Concatenating matrices Reshaping a matrix Inverse of a matrix Creating a matrix Julia provides a very simple notation to create matrices. A matrix can be created using the following notation: A = [1 2 3; 4 5 6]. Spaces separate entries in a row and semicolons separate rows. We can also get the size of a matrix using size(A). Transpose of a matrix The transpose operation flips the matrix over its diagonal by switching the rows and columns. Let A be a matrix. We can get the transpose of A by using A'. Example 1: Python3 1== # Defining a square matrix of size (2, 2) A = [1 2; 3 4] # Transpose of A A' Output: Example 2: Python3 1== # Defining a retangular matrix of size (2, 3) B = [1 2 3; 4 5 6] # Transpose of B B' Output: Flipping a matrix: A matrix in Julia can be flipped via the X-axis i.e. horizontally or via the Y-axis i.e. vertically. To flip the matrix we use reverse(< matrix >, dims= < 1 or 2 >)) 1 = vertically, 2 = horizontally. Example 1: Flipping vertically Python3 1== # Defining a rectangular matrix of size (2, 3) B = [1 2 3; 4 5 6] # Flipping the matrix vertically reverse(B, dims = 1) Output: Example 2: Flipping horizontally Python3 1== # Flipping the matrix horizontally reverse(B, dims = 2) Concatenating matrices In Julia we can concatenate a matrix to another matrix to the right side of the initial matrix or to the bottom of it. We use vcat(A, B) to concatenate to the side. And hcat(A, B) to concatenate to the bottom. While concatenating to the side, we need to make sure that both the matrices have same number of rows. While concatenating to the bottom, we need to make sure that both the matrices have same number of columns. Example 1: Concatenate to the side Python3 1== # Creating a square matrix of size (2, 2) A = [1 2; 3 4] # Creating a rectangular matrix of size (2, 3) B = [5 6 7; 8 9 10] hcat(A, B) Example 2: Concatenate to the bottom Python3 1== # Creating a square matrix of size (3, 2) A = [1 2;3 4; 5 6] # Creating a rectangular matrix of size (4, 2) B = [5 7;8 9; 10 11;14 16] vcat(A, B) Reshaping a matrix We can reshape a matrix into another matrix of different size. Example 1: Reshaping a matrix Python3 1== # The original matrix with size (3, 2) A = [1 2; 3 4; 5 6] Output: Reshaping the matrix to size (2, 3) Python3 1== reshape(A, (2, 3)) Output: Reshaping the matrix to size (6, 1) Python3 1== reshape(A, (6, 1)) Reshaping the matrix to size (1, 6) Python3 1== reshape(A, (1, 6)) Output: Inverse of a matrix If A is a square matrix its multiplicative inverse is called its inverse matrix. Denoted by A-1. In Julia we use inv(A) to get the inverse of the matrix A.Example 1: Getting the Inverse of a matrix Python3 1== # Creating a square matrix of size (2, 2) A = [4 7; 2 6] # Getting the inverse of matrix A inv(A) Example 2: Getting Identity Matrix Python3 1== # Creating a square matrix of size (2, 2) A = [4 7; 2 6] # Getting the Identity matrix A * inv(A) Comment More infoAdvertise with us Next Article Manipulating matrices in Julia K Kiza Follow Improve Article Tags : Julia Julia-matrix Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Like