Contents
introduction
display macro
opt={'delimiter','','whitespace',''};
mdis=@(x) disp(char(strread(evalc('evalin(''base'',x)'),'%s',opt{:})));
ND to 2D examples
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
r=reshapec(m);
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=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
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
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
r=reshapec(m,3,[],2,[]);
RESHAPEC> ERROR: too many occurrences of []
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
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