Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Thursday, 3 October 2013

How to reorganize the contents of an array/matrix in Matlab?

How to reorganize the contents of an array/matrix in Matlab?

Use 'reshape' command in Matlab.

>> help reshape


Example:

>> ss = magic(4)

ss =

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

>> d = reshape(ss,8,2)

d1 =

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

>> d2 = reshape(ss,2,8)

d2 =

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

Note: reshape works 'COLUMN' wise.

How to have more columns and rows, with different sizes, in SUBPLOT?


%Subplot with different sizes and shapes
figure;
subplot(2,1,1);
surf(magic(20));

subplot(2,3,4);
surf(magic(10));

%Increased size in x-direction
subplot(2,3,5:6);
surf(magic(10));

%Increased size in y-direction
figure;
subplot(10,2,1:6);
plot(sin(2*pi*5*(0:0.001:1)));

subplot(10,2,7:20);
surf(magic(10));

Note: The key idea is, first define the number of rows(R) and number of columns(C), for example subplot(R,C,xx:yy) and then play with the third entry (xx:yy) to define the sizes.

You can also control the 'position and size' of the subplot using:
subplot('position',[0 0 1 1]);

%This is bit involved as it demands you to know the co-ordinates and also the size