function [leftA, leftB, rightA, rightB, leftInd, rightInd] = ...
        permuteAndSplitData(A, B, ratio)
% Performs the same random permutation to two arrays of column vectors,
% splits the permuted columns into left and right parts, and returns also
% the indices corresponding to original locations of the data.
%
% Input:
%
%   A - one set of vectors as a column array
%   B - another set of vectors, must have the same number of columns as A
%   ratio - how big portion of the data will be in the left returns (0-1) 
%
% Return:
%
%   leftA    - floor(ratio * size(A,2)) randomly selected columns from A, in
%              random order
%   leftB    - floor(ratio * size(A,2)) columns from B, whose original indices
%              correspond to those returned in leftA
%   rightA   - the rest of columns from A, in random order
%   rightB   - the rest of columns from B, in the same random order as rightA
%   leftInd  - the original column indices that ended up in left splits
%   rightInd - the original column indices that ended up in left splits
%
% I use this to create many different test datasets from an original bulk.
%

if size(A,2) ~= size(B,2)
    error('permuteAndSplitData can be used only for same sized data');
end;

nLeft = floor(ratio*size(A,2));
rp    = randperm(size(A,2));

leftInd  = rp(1:nLeft);
rightInd = rp(nLeft+1:end);

leftA = A(:,leftInd);
leftB = B(:,leftInd);
rightA = A(:,rightInd);
rightB = B(:,rightInd);
