Contents

introduction

% RESHAPE
%		- changes the size of a matrix in ROW-COLUMN order.
%
% RESHAPEC
%		- changes the size of a matrix in COLUMN-ROW order.
%		- in particular, converts a ND to a 2D matrix
%		  according to MATLAB's display convention for
%		  easier reading and saving.
%
% SYNTAX
% ------------------------------------------------------------------------------
%		M2D = RESHAPEC(MND);
%		MND = RESHAPEC(M2D,NDSIZE);
%		MND = RESHAPEC(MND,NDSIZE);
%		MND = RESHAPEC(...,SIZE(1),SIZE(2),...,SIZE(N));
%
% INPUT
% ------------------------------------------------------------------------------
% MND	:	ND-matrix
% M2D	:	2D-matrix
% NDSIZE	:	array of new dimensions of MND or M2D
%		- the first number is #rows
%		- may include any number of singletons
% SIZE()	:	new size of dimension(X)
%		- dimension(1,1) is #rows
%		- one dimension can be a []
%		- may include any number of singletons
%		- may itself be a row or column vector
%
%		example of a valid NDSIZE/SIZE() combination
%		  RESHAPEC(MND,[1,1,2].',2,2,[3:5]);
%
%		if neither NDSIZE nor SIZE() are selected, the
%		input defaults to RESHAPEC(MND,[],#columns)
%
% OUTPUT
% ------------------------------------------------------------------------------
% M2D	:	2D conversion of MND following ML's display convention
% MND	:	ND conversion of MND or M2D according to NDSIZE or SIZE(1:N)

display macro

% removes superfluous spaces from disp(result)

	opt={'delimiter','','whitespace',''};
	mdis=@(x) disp(char(strread(evalc('evalin(''base'',x)'),'%s',opt{:})));

ND to 2D examples

% create a ND matrix

	ns=[2,3,2,2];
	m=reshape(1:prod(ns),ns);
	mdis('m');
m(:,:,1,1) =      
     1     3     5
     2     4     6
m(:,:,2,1) =      
     7     9    11
     8    10    12
m(:,:,1,2) =      
    13    15    17
    14    16    18
m(:,:,2,2) =      
    19    21    23
    20    22    24
% reshape ND(m) to 2D(m) according to ML display convention
% column-row

	r=reshapec(m);	% default equivalent of: reshapec(m,[],#columns)
	mdis('r');
r =               
     1     3     5
     2     4     6
     7     9    11
     8    10    12
    13    15    17
    14    16    18
    19    21    23
    20    22    24
% comparison with RESHAPE
% row-column

	r=reshape(m,[],3);
	mdis('r');
r =               
     1     9    17
     2    10    18
     3    11    19
     4    12    20
     5    13    21
     6    14    22
     7    15    23
     8    16    24

ND to ND examples

% reshape ND(m) to ND(m,newsize)
% note: outputs are equal because #rows does NOT change

	r=reshapec(m,2,[],4);
	mdis('r');
r(:,:,1) =        
     1     3     5
     2     4     6
r(:,:,2) =        
     7     9    11
     8    10    12
r(:,:,3) =        
    13    15    17
    14    16    18
r(:,:,4) =        
    19    21    23
    20    22    24
	r=reshape(m,2,[],4);
	mdis('r');
r(:,:,1) =        
     1     3     5
     2     4     6
r(:,:,2) =        
     7     9    11
     8    10    12
r(:,:,3) =        
    13    15    17
    14    16    18
r(:,:,4) =        
    19    21    23
    20    22    24
% reshape ND(m) to ND(m,newsize)
% note: outputs are NOT equal

	r=reshapec(m,[2,4],[]);
	mdis('r');
r(:,:,1) =              
     1     3     5     2
     4     6     7     9
r(:,:,2) =              
    11     8    10    12
    13    15    17    14
r(:,:,3) =              
    16    18    19    21
    23    20    22    24
	r=reshape(m,2,4,[]);
	mdis('r');
r(:,:,1) =              
     1     3     5     7
     2     4     6     8
r(:,:,2) =              
     9    11    13    15
    10    12    14    16
r(:,:,3) =              
    17    19    21    23
    18    20    22    24

singletons

	r=reshapec(m,[3,2],1,1,1,[2,2].');
	mdis('r');
r(:,:,1,1,1,1,1) =
     1     3      
     5     2      
     4     6      
r(:,:,1,1,1,2,1) =
     7     9      
    11     8      
    10    12      
r(:,:,1,1,1,1,2) =
    13    15      
    17    14      
    16    18      
r(:,:,1,1,1,2,2) =
    19    21      
    23    20      
    22    24      
	r=reshape(m,3,2,1,1,1,2,2);
	mdis('r');
r(:,:,1,1,1,1,1) =
     1     4      
     2     5      
     3     6      
r(:,:,1,1,1,2,1) =
     7    10      
     8    11      
     9    12      
r(:,:,1,1,1,1,2) =
    13    16      
    14    17      
    15    18      
r(:,:,1,1,1,2,2) =
    19    22      
    20    23      
    21    24      

error handling

% too many []

	r=reshapec(m,3,[],2,[]);
RESHAPEC> ERROR: too many occurrences of []
% invalid new size

	r=reshapec(m,6,2,5);
RESHAPEC> ERROR: cannot convert sizes!
RESHAPEC> old  :    24 = 2 3 2 2 
RESHAPEC> new  :    60 = 6 2 5 
	r=reshapec(m,6,[],5);
RESHAPEC> ERROR: cannot find a valid new size
RESHAPEC>   old: 24.00 = 2.00 3.00 2.00 2.00 
RESHAPEC>   new: 24.00 = 6.00 0.80 5.00 
% only one dimension
% note: RESHAPEC trys to compute the #columns

	r=reshapec(m,2);
	mdis('r');
r =                                                                     
     1     3     5     2     4     6     7     9    11     8    10    12
    13    15    17    14    16    18    19    21    23    20    22    24
	r=reshapec(m,13);
RESHAPEC> ERROR: cannot find a valid new size
RESHAPEC>   old: 24.00 = 2.00 3.00 2.00 2.00 
RESHAPEC>   new: 24.00 = 13.00 1.85