Ticker

6/recent/ticker-posts

How to convert array to matrix and manipulate to newer matrix in MATLAB?

Question:

How to convert array to matrix and manipulate to newer matrix in MATLAB?

Answer:

Given any array of numbers can be easily be convetred to matrix by using MATLAB built-in command "reshape". and then performing reshape command to each row/column and concatenate to a newer matrix. "flip" command also used for flipping column entries from bottom to top.

Example:

Let's create a array of natural numbers from 1 to 16 and store in A_old matrix and reshape each row according to the given condition

A = 1:16;
A_old = (reshape(A,[4,4]))'
A1 = reshape(A_old(:,1),[2,2])';
A2 = reshape(A_old(:,2),[2,2])';
A3 = reshape(flip(A_old(:,4)),[2,2])';
A4 = reshape(flip(A_old(:,3)),[2,2])';
A_new = [A1 A2;A3 A4]

where the second argument of reshape tells the dimension of matrix need to create. In this case, we are interested in 4x4 matrix

OUTPUT

A_old =

     1     2     3     4
     5     6     7     8
     9    10    11    12
    13    14    15    16


A_new =

     1     5     2     6
     9    13    10    14
    16    12    15    11
     8     4     7     3

Post a Comment

0 Comments