function E = FCN_switch(A,a,r)

% Funkcia sluzi na reorganizaciu matice(stlpcoveho vektora) A.
% Inputs:
%
%   A   -   Matica(stlpcovy vektor) ktoru reorganizujeme. 
%           Ak A je matica, musi byt stvorcova.
%   a   -   Vektor obsahujuci riadky/stlpce ktore chceme premiestnit 
%           dole/doprava resp. hore/dolava.
%           Tento vektor musi obsahovat presuvane riadky/stlpce v 
%           zostupnom poradi teda a(1) < a(2) < a(3) ... < ... a(n).
%   r   -   Argument typu string. Udava ktorym smerom chceme posunut 
%           riadky/stlpce matice A
%           r = 'up'    - posun hore/doprava
%           r = 'down'  - posun dole/doprava
%
% Outputs:
%
%   E   -   Reorganizovana matica(stlpcovy vektor) A
%
% Example:  Posunme stlpce/riadky c. 1, 2, 3, 31, 32 matice M hore/dolava:
%
%    M_mod = FCN_switch(M,[1 2 3 31 32],'up')
%
% Urcene pre vyuku predmetu MKP v dynamike struktur I, II
% Autor:    Vedelin Hok


[p q] = size(A);    % rozmery A
n = length(a);      % rozsah A


if q == 1 % A - stlpcovy vektor +++++
    B = A;
    for i = 0:n-1;
        B(a(i+1)-i,:) = [];
        C(i+1,:) = A(a(i+1),:);
    end

    down = strcmp(r,'down');
    if down == 1;       % down
        E = vertcat(B,C);
    else            % up       
        E = vertcat(C,B);
    end
else   % A - matica +++++++++++++++++
    % Zamena riadkov
    B = A;
    for i = 0:n-1;
        B(a(i+1)-i,:) = [];
        C(i+1,:) = A(a(i+1),:);
    end

    down = strcmp(r,'down');
    if down == 1;       % down
        D = vertcat(B,C);
    else            % up       
        D = vertcat(C,B);
    end

    % Zamena stlpcov
    B = D;
    clear C;
    for i = 0:n-1;
        B(:,a(i+1)-i) = [];
        C(:,i+1) = D(:,a(i+1));
    end

    down = strcmp(r,'down');
    if down == 1;       % right
        E = horzcat(B,C);
    else            % left    
        E = horzcat(C,B);
    end
end



