📄 reframearray.m
字号:
function ar = reframearray(ar, ardims, neutral)
% reframearray - reframes an array into new dims
%
% FORMAT: ar = reframearray(ar, ardims, neutral)
%
% Input fields:
%
% ar N-D data array
% ardims new dims
% neutral neutral element (for new entries)
%
% Note: existing data will be cut at the upper boundaries
% Version: v0.5c
% Build: 6120415
% Date: Dec-04 2006, 3:15 PM CET
% Author: Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% argument check
if nargin < 2 || ...
~isa(ardims, 'double') || ...
isempty(ardims) || ...
numel(ardims) ~= length(ardims) || ...
length(ardims) > 63 || ...
any(isinf(ardims) | isnan(ardims)) || ...
any(ardims ~= fix(ardims) | ardims < 0 | ardims > 1e9)
error( ...
'BVQXtools:BadArgument', ...
'Invalid dimensions or wrong number of arguments' ...
);
end
if nargin < 3
if isnumeric(ar) || ...
ischar(ar)
neutral = ar(1:min(1, numel(ar)));
neutral(1) = 0;
elseif iscell(ar)
neutral = {[]};
elseif islogical(ar)
neutral = false;
else
error( ...
'BVQXtools:TooFewArguments', ...
'No default neutral element defined for class %s.', ...
class(ar) ...
);
end;
end
% get current dims and get length of dims right
crdims = size(ar);
while ardims(end) == 1
ardims(end) = [];
if isempty(ardims)
break;
end
end
while length(ardims) < 2 || ...
length(ardims) < length(crdims)
ardims = [ardims(:)', 1];
end
while length(crdims) < length(ardims)
crdims = [crdims(:)', 1];
end
% leave if nothing changed
if all(crdims == ardims)
return;
end
% get dimlength and prepare subsasgn struct
dl = length(ardims);
ss.type = '()';
ss.subs = cell(1,dl);
% get to-shrink dims
smaller = find(ardims < crdims);
for sc = smaller
for dc = 1:dl
if dc == sc
ss.subs{dc} = (ardims(dc)+1):crdims(dc);
else
ss.subs{dc} = ':';
end
end
ar = subsasgn(ar, ss, []);
end
% expanding dims ?
if any(ardims > crdims)
for dc = 1:dl
ss.subs{dc} = ardims(dc);
end
% setting to neutral element should do the trick
ar = subsasgn(ar, ss, neutral);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -