## What is MATLAB Reshape?

There comes a time in every scientist and engineer’s life when all you live and breathe are input and output processes. Nature designs in such a manner, so naturally across the sciences the odds of encountering an input-output process are highly likely. In the advanced scientific sphere, there is often a transition from viewing systems as physical representations, to the view of those systems as their mathematical virtual twins. The MATLAB Reshape Array, is one tool that facilitates the mathematical manipulation of these virtual systems.

#### Table of Contents (click to navigate)

## Introduction to Reshape Array

There comes a time in every scientist and engineer’s life when all you live and breathe are input and output processes. Nature designs in such a manner, so naturally across the sciences the odds of encountering an input-output process are highly likely. In the advanced scientific sphere, there is often a transition from viewing systems as physical representations, to the view of those systems as their mathematical virtual twins. The MATLAB Reshape Array, is one tool that facilitates the mathematical manipulation of these virtual systems.

Over time, modelling any scientific and engineering system will be the norm for any mathematics, science or engineering base. Whether you’re an electrical engineer designing a motherboard, or a process control engineer designing a control system to manage system disturbances, or a biological engineer detecting whether or not feeding a fungus specie A will produce the desired synthesized chemical product, at some point you’ll be exposed to systems that take a feed, and produce a different outcome. The intermediary in the conversion, is known as the transforming function. In a physical system, the transforming function is usually a process, while in virtual reality, the transforming function is an algorithm.

When you take physical systems, and model them with mathematical models, computers will process the data as mathematical arrays. Based on the image above, your information will be converted into an array representing that input data, and then converted to another mathematical representation via the transforming function to the array that represents the output function. The transformation algorithms will be facilitated by systems such as MATLAB. As a user, you simply need to know how to activate them. As a matrix based system, MATLAB is adroit at facilitating array reshaping, which is the mechanism we just described above. In the tutorial below, we’ll identify how best to represent your data, and then transform it into different formats.

Because MATLAB has the tendency to be code-intensive, with the practice, over time you simply adjust to the system’s syntax. Just persist, and you’ll get it! Depending on the functions you are working with, there is usually more than one way to write the same function, or alternately, if there are additional dimensions to be added to the function, there will be additions to the primary code.

After completing this tutorial, you will learn:

- What is the MATLAB Reshape? This will be an introduction to the basic syntax.
- How to use the MATLAB Reshape? One detailed description of a simple example, which then builds up into more complex functions that will enable you to use the syntax in more advanced customizable mathematical, scientific and engineering functions.

Without any further ado, let’s get started!

## What is MATLAB Reshape?

MATLAB Reshape, is that tool in MATLAB that allows an input mathematical array to be converted into a different array, while at the same time preserving the elements of the original data. For various reasons, such as those that are outlined above, it is quite possible that as an analyst, you’ll feel the need to manipulate your data in order to identify how certain feeds to a system will impact the system’s downstream behavior. Via the right syntax, the outcome can be determined. MATLAB as a system has two primary means via which a user can execute the function. These are:

**g**

**B = reshape(A,sz)**

**B = reshape(A,sz1,…,szN)**Since there are two ways to facilitate the reshape function, let us see what they both mean. The first function: **B**** = reshape(****A****,****sz****)** is the function that you use to reshape a function A, utilizing a sizing vector. This sizing vector, is denoted by the code sz. B is the output. The function is written much like a mathematical equation, where the output variable is the first in the equation, and the operating functions are outlined in the following text. With B as the desired function, A will be the input function or the dependent variable, which will be converted by the sizing vector sz. The transformation will then proceed via the behind the scenes MATLAB algorithms, and determine the desired outcome of the operation.

The second operation: **B**** = reshape(****A****,****sz1,…,szN**) reshapes your function A, by an array function. The array is defined by the range sz1-by-…-by-szN. The individual dimensions of the array will be defined by the user. As the data is processed, the converted output will generated by MATLAB. Since as a user, you are entering code into the system, you do have some higher level control of how your data is being processed. If you so desire, you have to choose to either determine the dimension size of your converting array, or have MATLAB determine this function for you. What MATLAB will do, is ensure that there’s a good elemental match between the matrix A, and its associated sizing vector, sz.

## Brief Tutorial: MATLAB Reshape

By now, I’m sure you must be bubbling with excitement. In your minds, if you’re passionate about your field, you’ll be ready to figure out…”How do I apply all of this theory?” We will jump right in, by logging into our MATLAB system. For this tutorial, the version of MATLAB used is the R2019a, but feel free to try the trial versions of the R2018b, as well as the online version of MATLAB, if you want to test the system differences. Once you log into MATLAB via the access of your choice – either your industrial license or your educational license, you will be greeted by the following screen:

As the system beckons you to enter some code, your primary focus will be on the Command Window on the bottom right hand corner of your screen. Once the system itself has churned out the relevant output, feel free to use the tabs at the top: PLOTS and APPS, in order to manipulate your data output. You will be able to generate your graphical output there, and present your output like the professional that you either are…or are becoming. Working from the MATLAB workbook, we will start with our first objective.

## EXAMPLE ONE

Objective: To Reshape a Vector into a Matrix

From physics, vectors have both magnitude and direction, and matrices tend to be scalar functions. The question asks us to do the following:

Reshape a 1-by-10 vector into a 5-by-2 matrix.

**A = 1:10; …(1)**

**B = reshape(A,[5,2])…(2)**

Applying what we learned above, we see that A, the matrix to be converted has the dimensions of 1:10. Applying our reshape function, we will be apply the sizing vector (5,2) to convert the 1 by 10 matrix that is A, into a 5-by-2 matrix. After entering the two lines of code into our system, MATLAB generates the following output:

After MATLAB works its magic, you’ll see that the data itself was converted into the desired output. The process itself was straight forward enough, let’s jump into a higher intensity example. The next question in the workbook asks us to reshape a matrix to have a specified number of columns. If you recall from the syntax above, this will involve that section function, with the customizable sizing vector feature.

## EXAMPLE 2:

The question asks: Reshape a 4-by-4 square matrix into a matrix that has 2 columns. Specify [] for the first dimension to let reshape automatically calculate the appropriate number of rows.

The system code is entered as outlined below:

**A = magic(4)…(1)**

**B = reshape(A,[],2)…(2)**

The MATLAB system output is as follows:

Let’s dissect the code. The example above, gave us the magic matrix (4) in the first line of code. This magical matrix is one where the diagonal, and the rows add to give the same sum. We then instructed the system using the second line of code to reshape the vector. The sizing vector chosen was indicated by [] indicator, which allows MATLAB to determine its desired number of rows, while deciding which number of columns the system is to generate. The final output is the 8-by-2 matrix that you see in the answer at the bottom of the command window. As determined by the user, the elements of A were preserved, but the order of the data was rearranged as instructed by the user.

## CONCLUSIONS:

As you proceed on your MATLAB journey, you will be greeted by the opportunity to convert a series of matrices into alternate formats. Whether you want to test a system for any changes in the output based on a differing feed condition, or simply want to learn the syntax of the MATLAB system for your own personal scientific and mathematical education, then, by all means, explore the system. With arrays, as you’ve see above, the power is in your hands to manipulate the vectors and their output. Using either one of the MATLAB syntaxes outlined above, you’ll master the art of optimizing or manipulating input-output systems. Happy Coding!!