-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbst_bsxfun.m
65 lines (59 loc) · 2.6 KB
/
bst_bsxfun.m
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
function C = bst_bsxfun(fun, A, B)
% BST_BSXFUN: Compatible version of bsxfun function.
%
% DESCRIPTION:
% Matlab function bsxfun() is a useful, fast, and memory efficient
% way to apply element-by-element operations on huge matrices.
% The problem is that this function only exists in Matlab versions >= 7.4
% This function check Matlab version, and use bsxfun if possible, if not
% it finds another way to perform the same operation.
% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2010
% Old Matlab version: do it old school
if ~exist('bsxfun', 'builtin')
sA = [size(A,1), size(A,2), size(A,3)];
sB = [size(B,1), size(B,2), size(B,3)];
% If arguments were not provided in the correct order
if all(sA == [1 1 1]) || all(sB == [1 1 1])
C = fun(A, B);
elseif all(sA == sB)
C = fun(A, B);
% Dim 1
elseif (sB(1) == 1) && (sA(2) == sB(2)) && (sA(3) == sB(3))
C = fun(A, repmat(B, [sA(1), 1, 1]));
elseif (sA(1) == 1) && (sA(2) == sB(2)) && (sA(3) == sB(3))
C = fun(repmat(A, [sB(1), 1, 1]), B);
% Dim 2
elseif (sA(1) == sB(1)) && (sB(2) == 1) && (sA(3) == sB(3))
C = fun(A, repmat(B, [1, sA(2), 1]));
elseif (sA(1) == sB(1)) && (sA(2) == 1) && (sA(3) == sB(3))
C = fun(repmat(A, [1, sB(2), 1]), B);
% Dim 3
elseif (sA(1) == sB(1)) && (sA(2) == sB(2)) && (sB(3) == 1)
C = fun(A, repmat(B, [1, 1, sA(3)]));
elseif (sA(1) == sB(1)) && (sA(2) == sB(2)) && (sA(3) == 1)
C = fun(repmat(A, [1, 1, sB(3)]), B);
else
error('A and B must have enough common dimensions.');
end
% New Matlab version: use bsxfun
else
C = bsxfun(fun, A, B);
end