% eda13_03
% examples of reorganizing a matrix

clear all;

% define a matrix
N=3;
M=3;
A=[ [1,4,7]', [2,5,8]', [3,6,9']' ];
disp('a test matrix A');
A

% note that a matrix can be referred to using only a single
% index, in which case, it acts like a vector constructed
% by concatenating the columns of the matrix, one on top
% of the other
disp('A(4) is A(1,2)');
A(4)

% create a column-vector, b, containing the elements of A, organized
% column-wide.
b = A(:);
disp('transpose of a vector formed from A by method 1');
b'

% here's another way to do it
c = reshape(A,M*N,1);
disp('transpose of a vector formed from A by method 2');
c'

% the matrix reformed from the vector
B = reshape(c,N,M);
disp('the vector reformed from the vector');
B

% some indices into the matrix A
i= [1, 2, 3]';
j= [2, 3, 3]';
% corresponding index into the array b
k = sub2ind(size(A),i,j);
disp('corresponding elements of the matric and vector');
for q=[1:length(i)]
   disp(sprintf('A(%d,%d) = %f = b(%d) = %f', i(q), j(q), A(i(q),j(q)), k(q), b(k(q)) ));
end
