-
Notifications
You must be signed in to change notification settings - Fork 9
/
nt_mmat.m
54 lines (43 loc) · 1.23 KB
/
nt_mmat.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
function y=nt_mmat(x,m)
%y=nt_mmat(x,m) - matrix multiplication (with convolution)
%
% y: result
%
% x: input data (2D or more)
% m: matrix to apply (2D: right multiply, 3D: same with convolution)
%
% If m is 3D, the last index (k) is lag.
if nargin<2; error('!'); end
if iscell(x)
for iCell=1:length(x)
y{iCell}=nt_mmat(x{iCell},m);
end
return;
end
if ndims(x)>3
% concatenate the last dimensions, process, then de-concatenate
sz=size(x);
x=reshape(x,[sz(1),sz(2),prod(sz(3:end))]);
x=nt_mmat(x,m);
x=reshape(x,[size(x,1),sz(2),sz(3:end)]);
else
if ndims(m)==2;
% no convolution
y=nt_mmat0(x,m);
else
% does anyone use this ?????
[nRows,nCols,nLags]=size(m);
[nSamples,nChans,nTrials]=size(x);
if nChans~=nRows;
error('ncols(x) ~= nrows(m)');
end
% convolution: for each k, multiply x by m(:,:,k) and add with
% shift of (k-1)
y=zeros(nSamples+nLags-1,nCols,nTrials);
for iLag=1:nLags
y(iLag:iLag+nSamples-1,:,:) = y(iLag:iLag+nSamples-1,:,:) + nt_mmat0(x,m(:,:,iLag));
end
end
end
function x=nt_mmat0(x,m)
x=nt_fold(nt_unfold(x)*m,size(x,1));