-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathadd2Diag.m
More file actions
31 lines (26 loc) · 792 Bytes
/
Copy pathadd2Diag.m
File metadata and controls
31 lines (26 loc) · 792 Bytes
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
function out = add2Diag( A, v )
% out = add2Diag( A, v )
%
% Adds vector v to the diagonal elements of matrix A
%
% Inputs:
% A - a matrix of size M x N
% v - either a scalar or a vector with min(M,N) elements in it
%
% Written by Nicholas Dwork - Copyright 2019
%
% https://github.com/ndwork/dworkLib.git
%
% This software is offered under the GNU General Public License 3.0. It
% is offered without any warranty expressed or implied, including the
% implied warranties of merchantability or fitness for a particular
% purpose.
sA = size( A );
i = 1 : min( sA );
if numel( v ) > 1 && numel( i ) ~= numel( v )
error( 'v has the wrong number of elements' );
end
indxs = sub2ind( sA, i, i );
out = A;
out( indxs ) = out( indxs ) + v;
end