Matlab

How to Turn an Array into a Column Vector in MATLAB

MATLAB stands for matrix laboratory and it allows us to perform various array operations. Sometimes we need to change the dimension of an array by transforming it into a row or column vector and vice versa. Turning an array into a column vector is useful since it is easy to store and manipulate column vectors than row vectors.

This guide will help you to discover how to turn an array into a column vector in MATLAB.

How to Turn an Array into a Column Vector in MATLAB?

MATLAB supports the transformation of an array into a column vector from the following two methods:

1: How to Turn an Array into a Column Vector Using A(:) Operation?

We can transform an array into a column vector utilizing the A(:) operation in MATLAB. This operation converts all dimensions of an array into a single column.

Example

The given example uses the rand() function to create an array of random numbers having size 2-by-3-by-2. Then it uses the A(:) operation to convert this array into a column vector of size 1-by-12.

A = rand(2,3,2);

vect = A(:)

2: How to Turn an Array into a Column Vector Using the reshape() Function?

The reshape() is a built-in function in MATLAB that enables us to alter an array’s dimension. This function is used for converting an array into a column vector. This function takes two parameters as input and returns a column vector that is the transformation of the provided array and has all elements of the given array.

Syntax

To convert an array into a column vector, the reshape() function uses the following syntax:

vect = reshape(A,sz)

Here,

The function vect = reshape(A,sz) turns an array A into a column vector vect having specified size sz. The given array’s cardinality must be the same as the length of the column vector.

Example

The given MATLAB code uses the rand() function to create an array of random numbers having size 2-by-3-by-2. Then it uses the reshape() function to convert this array into a column vector of size 1-by-12.

A = rand(2,3,2);

vect = reshape(A,12,1)

Conclusion

MATLAB is a powerful programming tool that allows us to perform various array operations. It facilitates us to convert an array into a column vector using the A(:) operation and using the built-in reshape() function. These methods are equivalent to each other for turning an array into a column vector. This guide has discovered how to turn an array into a column vector in MATLAB through methods like A(:) operation and the built-in reshape() function.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.