Skip to content

Commit

Permalink
added vectorDemux.m
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele committed Apr 17, 2019
1 parent ffe1b47 commit ee922a4
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions library/matlab-wbc/+wbc/vectorDemux.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function varargout = vectorDemux(vector,partitionTable)

% VECTORDEMUX demuxes a vector according to a user-defined partition.
%
% FORMAT: varargout = vectorDemux(vector,partitionTable)
%
% INPUTS: - vector: [n * 1] the vector to demux.
% - partitionTable: a vector of positive integers that specifies
% how to demux the input vector.
%
% OUTPUTS: - the input vector demuxed according to the partition table.
%
% Author : Gabriele Nava ([email protected])
%
% Genova, Nov 2018

%% ------------Initialization----------------

% verify the partition table is valid
if sum(partitionTable) ~= length(vector)

error('[vectorDemux]: invalid partition table.')
end

% add an extra 0 as first element of the partitionTable to simplify
% the input vector demux procedure
if size(partitionTable,1) == 1

partitionTable = [0, partitionTable];
else
partitionTable = [0; partitionTable];
end

% partition the input vector
sumPartitionTable = 0;

for k = 2:length(partitionTable)

% zeros, non integer and negative elements are not allowed
if partitionTable(k) <= 0 || partitionTable(k) ~= round(partitionTable(k))

error('[vectorDemux]: the partition table contains an invalid element.');
end

% cumulate the values of the partition table
sumPartitionTable = sumPartitionTable + partitionTable(k-1);
varargout{k-1} = vector(1+sumPartitionTable:sumPartitionTable+partitionTable(k)); %#ok<AGROW>
end
end

0 comments on commit ee922a4

Please sign in to comment.