Skip to content

Commit be9e227

Browse files
authored
Initial main file commit
1 parent 24de2fd commit be9e227

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed

samesize.m

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
%% Takes any number of cell or double arrays and resizes them all to the same dimensions. Also serves to resize any array with removal of extra rows/columns and adding of NaN, 0, or empty string rows/columns.
2+
3+
% Matrices can be truncated to match the smallest dimensions of the
4+
% matrices, or extended with a filler value to match the largest dimensions
5+
% of the matrices. For this behavior, the program will find the
6+
% minimum and maximum number of rows and columns from all the inputted
7+
% arrays and use those to set the dimensions of all arrays depending on
8+
% whether "small" or "big" was selected in the options. Default is "big".
9+
10+
% Matrices can also be resized to certain inputted dimensions, in which case too large dimesions will be truncated and too small will be filled.
11+
% The matrix filler defaults to NaN but can also be set to zero or an empty string.
12+
% One output matrix is given for every input matrix.
13+
% You can even mix inputs of double arrays and cell arrays and each array will be passed back in the same form it entered.
14+
% See arguments notes below.
15+
16+
% varargin: Any number of arrays of either class double or cell can be passed in. They can be of any size. One output is created for each input array.
17+
18+
% Name-Value Pair Arguments:
19+
% RowCol: Manually sets the desired size of the output matrices. Must be submitted as a 1x2 non-zero integer vector indicating [rowsize, columnsize].
20+
% SmallBig: Sets whether to truncate larger matrices to the size of the smallest dimesions of any matrix, or extend smaller matrices to the size of the largest. Must be either "small" or "big". Defaults to "big". If the rows and columns have been manually set, this input is ignored.
21+
% Filler: Sets whether to fill extra rows and columns with NaN, zero, or an empty string (only for cell arrays). Must be "NaN", "nan", "zero", "0", "String", or "string". Defaults to "NaN".
22+
23+
% Outputs:
24+
% varargout: Ouputs one array for each array in the input. They can be accessed in the order they were entered. You do not necessarily have to take every output that is generated.
25+
% Example: [a,b] = samesize(a,b,c,d) will return output for a and b but not c and d. However, if you want just c and d, the call must look like [~,~,c,d] = samesize(a,b,c,d).
26+
27+
% Example function call with minimum arguments:
28+
% samesize(a,b) (where a and b are arrays of class cell or double)
29+
% In this case, default behavior will resize a and b to the largest dimensions of the arrays.
30+
31+
% With output:
32+
% [a,b] = samesize(a,b)
33+
34+
% With optional arguments for manual dimensions:
35+
% [a,b] = samesize(a,b,"RowCol", [3,4])
36+
37+
% With optional argument for shrinking to smallest or expanding to biggest:
38+
% [a,b] = samesize(a,b,"SmallBig", "small")
39+
40+
% With optional argument for filler type:
41+
% [a,b] = samesize(a,b,"Filler","zero")
42+
43+
% A note on behavior:
44+
% As mentioned above, the default response of the program is to make all
45+
% arrays the size of the largest dimensions of the arrays given. For
46+
% example, if you submit a 1x3 matrix and a 3x1 matrix, the result will be
47+
% 3x3 matrices with 2 rows of NaN for the first matrix and two columns of
48+
% NaN for the second, as this is the largest combination of rows and
49+
% columns of the matrices given. Likewise, if you submit a 3x3 matrix and
50+
% a 1x3 matrix, the result will be 3x3 matrices with no changes to the
51+
% first matrix and two rows of NaN for the second.
52+
53+
% This same behavior applies to the "small"
54+
% option. (i.e. an input of a 2x3 matrix and a 3x2 matrix with "small"
55+
% selected will output 2x2 matrices with the extra column of the first
56+
% matrix and the extra row of the second removed.)
57+
58+
function varargout = samesize(varargin,options)
59+
60+
arguments (Repeating)
61+
62+
varargin (:,:) {mustBeA(varargin, ["double", "cell"])}
63+
64+
end
65+
66+
% Specify input arguments, name-value pairs, and default values.
67+
arguments
68+
69+
options.RowCol (1,2) {mustBeNumeric,mustBeReal,mustBeInteger} = [0,0]
70+
options.SmallBig {mustBeMember(options.SmallBig,["small","big"])} = "big"
71+
options.Filler {mustBeMember(options.Filler, ["NaN", "nan", "0", "zero", "string", "String"])} = "NaN"
72+
73+
end
74+
75+
nOutputs = nargout;
76+
77+
if options.Filler == "NaN" || options.Filler == "nan"
78+
filler = NaN;
79+
elseif options.Filler == "string" || options.Filler == "String"
80+
filler = "";
81+
else
82+
filler = 0;
83+
end
84+
85+
if options.RowCol(1) ~= 0 && options.RowCol(2) ~= 0
86+
% fprintf("Resizing arrays...")
87+
for i=1:size(varargin, 2)
88+
[numrows, numcols] = size(varargin{i});
89+
rowdiff = numrows - options.RowCol(1);
90+
coldiff = numcols - options.RowCol(2);
91+
% Fix rows.
92+
if rowdiff > 0
93+
for rd=rowdiff:-1:1
94+
varargin{i}((options.RowCol(1)+rd), :) = [];
95+
end % For rd
96+
elseif rowdiff < 0
97+
for rd=1:abs(rowdiff)
98+
if class(varargin{i}) == "double"
99+
if class(filler) == "string"
100+
error("ERROR in sizesame. Input arrays included a double matrix, but filler was set to 'string'. Cannot populate double arrays with empty strings. Please set argument 'Filler' to either 'NaN' or 0 or change the matrix to a cell matrix.");
101+
end
102+
varargin{i}((numrows+rd), :) = filler;
103+
elseif class(varargin{i}) == "cell"
104+
varargin{i}((numrows+rd), :) = {filler};
105+
end
106+
end % For rd
107+
end
108+
109+
% Fix columns.
110+
if coldiff > 0
111+
for cd=coldiff:-1:1
112+
varargin{i}(:, (options.RowCol(2)+cd)) = [];
113+
end
114+
elseif coldiff < 0
115+
for cd=1:abs(coldiff)
116+
if class(varargin{i}) == "double"
117+
if class(filler) == "string"
118+
error("ERROR in sizesame. Input arrays included a double matrix, but filler was set to 'string'. Cannot populate double arrays with empty strings. Please set argument 'Filler' to either 'NaN' or 0 or change the matrix to a cell matrix.");
119+
end
120+
varargin{i}(:, (numcols+cd)) = filler;
121+
elseif class(varargin{i}) == "cell"
122+
varargin{i}(:, (numcols+cd)) = {filler};
123+
end
124+
end % For cd
125+
end
126+
end % For i
127+
% fprintf("Done\n");
128+
129+
elseif options.RowCol(1) ~= 0 && options.RowCol(2) == 0
130+
error("ERROR in sizesame(). At least one value in RowCol was zero. All values of RowCol must be nonzero to use manual sizing.");
131+
132+
elseif options.RowCol(1) == 0 && options.RowCol(2) ~= 0
133+
error("ERROR in sizesame(). At least one value in RowCol was zero. All values of RowCol must be nonzero to use manual sizing.");
134+
135+
else
136+
if length(varargin) < 2
137+
warning("WARNING in sizesame(). Only one matrix passed in, but RowCol was unset. Default behavior for this case is to pass back the original matrix unaltered. If you meant to change the size of this single matrix, set the 'RowCol' argument. If you meant to match this matrix size to another matrix (or vice versa), pass in multiple matrices.");
138+
else
139+
min_row = inf;
140+
min_col = inf;
141+
max_row = 0;
142+
max_col = 0;
143+
% Get minimum and maximum sizes of arrays.
144+
% fprintf("Finding minimum and maximum sizes...\n")
145+
for i=1:size(varargin, 2)
146+
if size(varargin{i}, 1) < min_row
147+
min_row = size(varargin{i}, 1);
148+
end
149+
if size(varargin{i}, 1) > max_row
150+
max_row = size(varargin{i},1);
151+
end
152+
if size(varargin{i}, 2) < min_col
153+
min_col = size(varargin{i}, 2);
154+
end
155+
if size(varargin{i}, 2) > max_col
156+
max_col = size(varargin{i}, 2);
157+
end
158+
end % For i
159+
% fprintf("Done\n");
160+
161+
% Fix dimensions.
162+
% fprintf("Resizing arrays...\n")
163+
if options.SmallBig == "small"
164+
for i=1:size(varargin, 2)
165+
[numrows, numcols] = size(varargin{i});
166+
rowdiff = numrows - min_row;
167+
if rowdiff > 0
168+
for rd=rowdiff:-1:1
169+
varargin{i}((min_row+rd), :) = [];
170+
end
171+
end
172+
coldiff = numcols - min_col;
173+
if coldiff > 0
174+
for cd=coldiff:-1:1
175+
varargin{i}(:, (min_col+cd)) = [];
176+
end
177+
end
178+
end % For i
179+
else
180+
for i=1:size(varargin, 2)
181+
[numrows, numcols] = size(varargin{i});
182+
rowdiff = max_row - numrows;
183+
if rowdiff > 0
184+
for rd=1:rowdiff
185+
if class(varargin{i}) == "double"
186+
if class(filler) == "string"
187+
error("ERROR in sizesame. Input arrays included a double matrix, but filler was set to 'string'. Cannot populate double arrays with empty strings. Please set argument 'Filler' to either 'NaN' or 0 or change the matrix to a cell matrix.");
188+
end
189+
varargin{i}((numrows+rd), :) = filler;
190+
elseif class(varargin{i}) == "cell"
191+
varargin{i}((numrows+rd), :) = {filler};
192+
end
193+
end % For rd
194+
end
195+
coldiff = max_col - numcols;
196+
if coldiff > 0
197+
for cd=1:coldiff
198+
if class(varargin{i}) == "double"
199+
if class(filler) == "string"
200+
error("ERROR in sizesame. Input arrays included a double matrix, but filler was set to 'string'. Cannot populate double arrays with empty strings. Please set argument 'Filler' to either 'NaN' or 0 or change the matrix to a cell matrix.");
201+
end
202+
varargin{i}(:, (numcols+cd)) = filler;
203+
elseif class(varargin{i}) == "cell"
204+
varargin{i}(:, (numcols+cd)) = {filler};
205+
end
206+
end % For cd
207+
end
208+
end % For i
209+
end
210+
% fprintf("Done\n");
211+
end
212+
213+
end
214+
215+
% fprintf("Assigning outputs...\n");
216+
varargout = cell(1,nOutputs);
217+
for n=1:nOutputs
218+
varargout{n} = varargin{n};
219+
end % For n
220+
% fprintf("Done\n");
221+
222+
end
223+
224+
225+
% samesize.m Resizing and size matching of arrays for MATLAB.
226+
% Copyright (C) 2022 Rhys Switzer
227+
%
228+
% This program is free software: you can redistribute it and/or modify
229+
% it under the terms of the GNU General Public License as published by
230+
% the Free Software Foundation, either version 3 of the License, or
231+
% (at your option) any later version.
232+
%
233+
% This program is distributed in the hope that it will be useful,
234+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
235+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
236+
% GNU General Public License for more details.

0 commit comments

Comments
 (0)