-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormaSort.m
More file actions
39 lines (34 loc) · 839 Bytes
/
normaSort.m
File metadata and controls
39 lines (34 loc) · 839 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
32
33
34
35
36
37
38
39
function[Aout] = normaSort(A)
%------------------------------------------------------------------------
%
% normaSort.m:
% Given a matrix, normalize each row so that max is 1, and arrange rows
% such that the 1s are along the diagonal. Linearly comparable in
% computational efficiency to matlab's sort().
%
% Inputs:
% A: The original matrix
%
% Outputs:
% Aout: The reorganized matrix
%
%------------------------------------------------------------------------
% Init
Aout = A;
n = size(A, 1);
% Normalize
rowmax = max(A, [], 2);
[rowabsmax ix] = max(abs(A), [], 2);
A = diag(1./rowabsmax)*A;
for i=1:n
if rowmax(i) ~= rowabsmax(i)
% Maximum absolute value was originally negative. Flip row.
A(i,:) = -1*A(i,:);
end
end
% Reorganize
[b iy] = sort(ix);
for i=1:n
Aout(i,:) = A(iy(i),:);
end
end