forked from robotology/whole-body-controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorDemux.m
49 lines (38 loc) · 1.65 KB
/
vectorDemux.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 (gabriele.nava@iit.it)
%
% 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