-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormal.m
executable file
·44 lines (40 loc) · 891 Bytes
/
normal.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
% function [nz,mz]=normal(z,dim)
% author: Ainray
% date: 20160130
% version: 1.0
% bug-report: wwzhang0421@163.com
% introduction: normalizing matrix by element with maximum absoulte value
% input:
% z, original matrix
% dim, if 1, matrix arranged by column, the default value
% if 2, matrix arranged by row
% output:
% nz, return normalized matrix
% mz, return vector containing maximum absolute values,
% by columns if dim==1, by rows if dim==2
function [nz,mz]=normal(z,dim)
if nargin<2
dim=1;
end
[m,n]=size(z);
if m==1 || n==1
z = z(:);
dim = 1;
[m, n] = size(z);
end
if dim~=1 && dim~=2
error('Dimension must be 1 or 2.');
end
if dim==2
z=z';
[m, n] = size(z);
end
[mv,I] = max(abs(z));
indx = I + (0:n-1)*m;
mz=z(indx);
mmat=ones(m,1)* mv(:)';
nz=z./mmat;
if dim==2
nz=nz';
mz=mz';
end