-
Notifications
You must be signed in to change notification settings - Fork 5
/
diagmult.m
43 lines (38 loc) · 972 Bytes
/
diagmult.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
% DIAGMULT Computes either diag(a)*b or a*diag(b)
% USAGE
% c=diagmult(a,b);
% INPUTS
% a,b : m-vector and mxn matrix
% or
% mxn matrix and n-vector
% OUTPUT
% c : mxn matrix
%
% Coded as a C-MEX function for speed
% Note: C version is not implemented for complex matrices or matrices
% with data type other than double. The vector must be full but
% the matrix can be sparse or full.
% Copyright (c) 2005-8, Paul L. Fackler, NCSU
% paul_fackler@ncsu.edu
function c=diagmult(a,b)
[m,n]=size(a);
% a is a vector
if m==1 | n==1
n=length(a);
if size(b,1)~=n
error('Inputs are not compatible')
end
c=sparse(1:n,1:n,a,n,n)*b;
else
[m,n]=size(b);
% b is a vector
if m==1 | n==1
n=length(b);
if size(a,2)~=n
error('Inputs are not compatible')
end
c=a*sparse(1:n,1:n,b,n,n);
else
error('Either a or b must be vectors')
end
end