User:Mate2code/code

(Redirected from User:Lipedia/code)

When I have self written programs that are of possible interest to others I make them available here.

perm2invset

```function [y] = perm2invset(x)
% Input: permutation of 1...n as row vector
% Output: binary row vector with nchoosek(n,2) places describing the inversion set
n = size(x,2) ;
m = nchoosek(n,2) ;
M = sortrows(   nchoosek(1:n,2)  ,  2  ) ;
Y = zeros(1,m) ;
for a=1:m
if x(M(a,1)) > x(M(a,2))
Y(a) = 1 ;
end
end
y = Y ;
end
```

perm2inv

```function [y] = perm2inv(x)
% Permutation to inversion vector
wide = size(x,2) ;
Y = zeros(1,wide) ;
for m=1:wide
for n=1:m-1
if x(n) > x(m)
Y(m) = Y(m)+1 ;
end
end
end
y = Y ;
end
```

cv2wp

```function [y] = cv2wp(x)
% Input: Compression vector (row)
% Output: Walsh permutation (row)

%   x   =   7   5   6

Small = size(x,2) ;
Big = 2^Small ;

%   Small   =   3
%   Big     =   8

X = cv2cm(x) ;

%   X   =   1   1   0
%           1   0   1
%           1   1   1

Bincount = ones(Small,Big) ;
for m=1:Small
for n=1:Big
if mod(  floor( (n-1)/2^(m-1) )  , 2) == 0
Bincount(m,n) = 0 ;
end
end
end

%   Bincount   =   0   1   0   1   0   1   0   1
%                  0   0   1   1   0   0   1   1
%                  0   0   0   0   1   1   1   1

Binwalsh = zeros(Small,Big) ;
for n=1:Small
N = zeros(1,Big) ;
for m=1:Small
if X(m,n) == 1
N = xor ( N , Bincount(m,:) ) ;
end
end
Binwalsh(n,1:Big) = N ;
end

%   Binwalsh   =   0   1   1   0   1   0   0   1
%                  0   1   0   1   1   0   1   0
%                  0   0   1   1   1   1   0   0

WP = zeros(1,Big) ;
for n=1:Big
N = 0 ;
for m=1:Small
if Binwalsh(m,n) == 1
N = N + 2^(m-1) ;
end
end
WP(n) = N ;
end

%        WP   =    0   3   5   6   7   4   2   1

y = WP ;

end
```

wp2cv

```function [y] = wp2cv(x)
% Walsh permutation to compression vector

% Used: char2mat, walshf2num

Bin = char2mat(dec2bin(x)) ;
Short = size(Bin,2) ;
Y = zeros(1,Short) ;
for m=1:Short
Y(Short+1-m) = walshf2num( Bin(:,m)' ) ;
end
y = Y ;

end
```

seq2mat

```function [y] = seq2mat(x)
% puts sequence x in the antidiagonals of a square matrix y

if size(x,1) > 1
x = x' ;
end
s = size(x,2) ;    % s is the length of the string

m = 1 ;
while (m^2+m)/2 <= s
m = m+1 ;
end
m = m-1 ;          % m is the size of the square matrix

if (m^2+m)/2 < s      % maybe s is no triangular number
s = (m^2+m)/2 ;    % now it is
end

I = zeros(s,2) ;   % I lists the places in the matrix M where the elements of the sequence x go.
i=1; j=0;
for a=1:m
j=j+a;
A = 1:a ;
I(i:j,1:2) = [ A' rot90(A) ] ;
i=i+a;
end

M = zeros(m) ;     % create mxm matrix M
for m=1:s
M( I(m,1) , I(m,2) ) = x(m) ;
end

y = M ;

end
```

mat2seq

```function [y] = mat2seq(x)
% orders the antidiagonals of the matrix in a sequence

high=size(x,1); long=size(x,2);
if high<long
m=high; M=x(1:m,1:m);
elseif long<high
m=long; M=x(1:m,1:m);
else m=high; M=x;
end                     % m is the size of the square matrix M

s = (m^2+m)/2 ;         % s is the length of the sequence S
S = zeros(1,s) ;

I = zeros(s,2) ;   % I lists the places in the matrix M where the elements of the antidiagonals are found
i=1; j=0;
for a=1:m
j=j+a;
A = 1:a ;
I(i:j,1:2) = [ A' rot90(A) ] ;
i=i+a;
end

for a=1:s
S(a) = M( I(a,1) , I(a,2) ) ;   % creation of sequence S
end

y = S ;

end
```

A function mat2seq_sym for symbolic inputs can be created by changing `S=zeros(1,s);` to `S=sym(zeros(1,s));`.

walshf2num

```function [y] = walshf2num(x)
% Binary Walsh function (row vector) to row number in Walsh matrix
% Function doesn't test if the input actually is a Walsh function,
% only counts changes between 0 and 1 and uses the fact that
% Gray code permutation * bit reversal permutation
% permutes the natural ordered Hadamard matrix (Walsh matrix)
% in the sequency ordered Hadamard matrix.

Long = size(x,2) ;
Short = log2(Long) ;

Changes = zeros(1,Long-1) ;
for m = 1:Long-1
Changes(m) = xor( x(m) , x(m+1) ) ;
end
Changes = sum(Changes) ;

Gray = bitxor( Changes , floor(Changes/2) ) ;  % element on place "Changes" in Gray code permutation

y = bin2dec( fliplr( dec2bin(Gray,Short) ) ) ;  % bit reversal of "Gray"

end
```

char2mat

```function [y] = char2mat(x)
% Turns a matrix of type CHAR without spaces
% into a matrix of type DOUBLE.
high = size(x,1) ;
wide = size(x,2) ;
Y = zeros(high,wide) ;
for m = 1:high
for n = 1:wide
Y(m,n) = str2double( x(m,n) ) ;
end
end
y = Y ;
end
```