Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[8d7bed]: scalardistribute.m Maximize Restore History

Download this file

scalardistribute.m    76 lines (67 with data), 2.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function varargout=scalardistribute(varargin)
%SCALARDISTRIBUTE Copy scalar to array shape for parameter handling
% Usage: [...] = scalardistribute(...);
%
% `[...]=scalardistribute(...)` copies the input parameters to the
% output parameters.
%
% * If one of the input parameters is an array, all the output parameters
% will be column vectors containing the same number of elements. If one
% of the other input parameters is a scalar, it will be replicated to
% the correct length. This allows a scalar value to be repeated for
% all conditions.
%
% * If two or more input parameters are arrays, the must have the exact
% same size. They will be converted to vectors and returned in the
% output parameters. This allows two arrays to co-vary at the same time.
%
% This operator is usefull for sanitizing input parameters: The user is
% allowed to enter scalars or arrays as input paremeters. These input
% are in turn passed to `scalardistribute`, which makes sure that the
% arrays have the same shape, and that scalars are replicated. The user
% of `scalardistibute` can now generate conditions based on all the
% parameters, and be sure the have the right sizes.
%
% As an example, consider:::
%
% [a,b,c]=scalardistribute(1,[2,3],[4,5])
%
npars=length(varargin);
scalartest=zeros(npars,1);
for ii=1:npars
if ~isnumeric(varargin{ii})
error('%s: Input no. %i must be numerical.',upper(mfilename),ii);
end;
if isempty(varargin{ii})
error('%s: Input no. %i is empty.',upper(mfilename),ii);
end;
scalartest(ii)=isscalar(varargin{ii});
end;
idx=find(scalartest==0);
% Tjeck that all non-scalars have the same shape
for jj=1:numel(idx)
ref_size = size(varargin{idx(1)});
this_size = size(varargin{idx(jj)});
if any((ref_size-this_size)~=0)
error('%s: Input no. %i and no. %i must have the same shape.',upper(mfilename),idx(1),idx(jj));
end;
end;
if numel(idx)==0
% All arguments are scalar, so this is just a dummy, but it must
% still be defined for the code not to fail.
shape=1;
else
shape=ones(numel(varargin{idx(1)}),1);
end;
varargout=cell(1,npars);
for ii=1:npars
if scalartest(ii)
% Replicate scalar
varargout{ii}=shape*varargin{ii};
else
% Copy input and turn it into a vector. This would be a one-liner
% in Octave.
tmp=varargin{ii};
varargout{ii}=tmp(:);
end;
end;