
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Turn an Array into a Column Vector in MATLAB
In MATLAB, an array is a data structure used to store elements in contiguous memory blocks. A column vector is a one?dimensional array of elements. In a column vector, the elements are arranged column?wise.
It is also important to note that MATLAB by default stores arrays in a column wise manner which means the elements of the array are stored in the memory as a column vector.
In this article, we will explore different approaches to convert an array into a column vector using MATLAB programming.
Method (1): Using Transpose Function
We can turn an array into a column vector by using the ?transpose' function, which is a built?in function of MATLAB.
The following MATLAB program demonstrates the use of the ?transpose' function to turn an array into a column vector.
Example
% MATLAB code for converting an array into a column vector using transpose function % Define an array a = [10, 20, 30, 40, 50, 60, 70]; % Turn the array into a column vector c = transpose(a); % Display the column vector disp('Column vector is: '); disp(c);
Output
Column vector is: 10 20 30 40 50 60 70
Explanation
In this MATLAB program, we demonstrated the conversion of a one?dimensional array into a column vector by using the ?transpose' function.
In this code, we start by creating a one?dimensional array of 7 elements. Next, we turn it into a column vector by using the ?transpose' function. Finally, we display the result using the ?disp' function.
The important thing to note about the ?transpose' function is that it can convert only a one?dimensional array into a column vector.
Method (2): Using Transpose Operator
We can also turn an array into a column vector by using the transpose operator ?''. Similar to the transpose function, the transpose operator is also used to turn a one?dimensional array into a column vector.
The following MATLAB program demonstrate the use of the transpose operator to convert an array into a column vector.
Example
% MATLAB code for converting an array into a column vector using transpose operator % Define an array a = [10, 20, 30, 40, 50, 60, 70]; % Turn the array into a column vector c = a.'; % Display the column vector disp('Column vector is: '); disp(c);
Output
Column vector is: 10 20 30 40 50 60 70
Explanation
The execution this MATLAB code is similar to that of the MATLAB program (1). The only difference is that in this code we have used the transpose operator ?'' instead of the transpose function.
Method (3): Using the Colon Operator
MATLAB also allows us to turn an array into a column vector by using the colon operator ?:'. The important point to note about the colon operator is that it can convert a multi?dimensional array into a column vector that the transpose operator cannot do.
The following two MATLAB programs demonstrate the use of the colon operator to convert a one?dimensional array and a two?dimensional array into column vectors respectively.
Example
% MATLAB code for converting an array into a column vector using colon operator % Define a one-dimensional array a = [10, 20, 30, 40, 50, 60, 70]; % Turn the array into a column vector c = a(:); % Display the column vector disp('Column vector is: '); disp(c);
Output
Column vector is: 10 20 30 40 50 60 70
Explanation
In this MATLAB code, we have used the colon operator ?:' to turn a one?dimensional array into a column vector. The expression ?c = a(:);' in the above code, turn the array ?a' into a column vector and store the result in the ?c' variable.
Example
% MATLAB code for converting an array into a column vector using colon operator % Define a two-dimensional array a = [10 20 30; 40 50 60; 70 80 90]; % Turn the array into a column vector c = a(:); % Display the column vector disp('Column vector is: '); disp(c);
Output
Column vector is: 10 40 70 20 50 80 30 60 90
Explanation
This MATLAB code turns a two?dimensional array into a column vector by using the colon operator ?:'.
Method (4): Using the Reshape Function
We can also convert an array into a column vector by using the ?reshape()' function, which is a built?in function of MATLAB. Similar to the colon operator, the reshape function can be also use to convert a multi?dimensional array into a column vector.
The following MATLAB programs demonstrate the use of the ?reshape' function to convert a one?dimensional array and a two?dimensional array into column vectors respectively.
Example
% MATLAB code for converting an array into a column vector using the reshape function % Define a one-dimensional array a = [10 20 30 40 50 60 70]; % Determine the number of elements in the array n = numel(a); % Turn the array into a column vector c = reshape(a, n, 1); % Display the column vector disp('Column vector is: '); disp(c);
Output
Column vector is: 10 20 30 40 50 60 70
Explanation
In this MATLAB code, we start by creating a one?dimensional array and store it in the ?a' variable. Then, we get the number of elements in the array by using the ?numel' function and store the result in the ?n' variable.
Next, we use the ?reshape' function to turn the array into a column vector. Finally, we display the resulting column vector ?c' by using the ?disp' function.
Example
% MATLAB code for converting an array into a column vector using reshape function % Define a two-dimensional array a = [10 20 30; 40 50 60; 70 80 90]; % Determine the number of elements in the array n = numel(a); % Turn the array into a column vector c = reshape(a, n, 1); % Display the column vector disp('Column vector is: '); disp(c);
Output
Column vector is: 10 40 70 20 50 80 30 60 90
Explanation
This MATLAB code demonstrates the conversion of a two?dimensional array into a column vector using the ?reshape' function. The execution of this code is same as that of the MATLAB program (5).
Conclusion
Hence, this is all about turning an array into a column vector. MATLAB provides various operators and built?in functions to turn an array into a column vector. All these approaches we covered in the above sections of this article with the help of MATLAB programs.