Mercurial > matrix-functions
comparison funm_files/swapping.m @ 0:8f23314345f4 draft
Create local repository for matrix toolboxes. Step #0 done.
author | Antonio Pino Robles <data.script93@gmail.com> |
---|---|
date | Wed, 06 May 2015 14:56:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8f23314345f4 |
---|---|
1 function [M,ind,n_swaps] = swapping(m) | |
2 %SWAPPING Confluent permutation by swapping adjacent elements. | |
3 % [ISWAP,IND,N_SWAPS] = SWAPPING(M) takes a vector M containing | |
4 % the integers 1:k (some repeated if K < LENGTH(M)) | |
5 % and constructs a swapping scheme that produces | |
6 % a confluent permutation, with elements ordered by ascending | |
7 % average position. The confluent permutation is obtained by using | |
8 % the LAPACK routine ZTREX to move m(ISWAP(i,2)) to m(ISWAP(i,1)) | |
9 % by swapping adjacent elements, for i = 1:SIZE(M,1). | |
10 % The cell array vector IND defines the resulting block form: | |
11 % IND{i} contains the indices of the i'th block in the permuted form. | |
12 % N_SWAPS is the total number of swaps required. | |
13 | |
14 mmax = max(m); M = []; ind = {}; h = zeros(1,mmax); | |
15 g = zeros(1,mmax); | |
16 | |
17 for i = 1:mmax | |
18 p = find(m==i); | |
19 h(i) = length(p); | |
20 g(i) = sum(p)/h(i); | |
21 end | |
22 | |
23 [x,y] = sort(g); | |
24 mdone = 1; | |
25 | |
26 for i = y | |
27 if any(m(mdone:mdone+h(i)-1) ~= i) | |
28 f = find(m==i); g = mdone:mdone+h(i)-1; | |
29 ff = f(f~=g); gg = g(f~=g); | |
30 | |
31 % Create vector v = mdone:f(end) with all elements of f deleted. | |
32 v = mdone-1 + find(m(mdone:f(end)) ~= i); | |
33 | |
34 % v = zeros(1,f(end)-g(1)+1); | |
35 % v(f-g(1)+1) = 1; v = g(1)-1 + find(v==0); | |
36 | |
37 M(end+1:end+length(gg),:) = [gg' ff']; | |
38 | |
39 m(g(end)+1:f(end)) = m(v); | |
40 m(g) = i*ones(1,h(i)); | |
41 ind = cat(2,ind,{mdone:mdone+h(i)-1}); mdone = mdone + h(i); | |
42 else | |
43 ind = cat(2,ind,{mdone:mdone+h(i)-1}); mdone = mdone + h(i); | |
44 end | |
45 end | |
46 | |
47 n_swaps = sum(abs(diff(M'))); |