Mercurial > repos > public > sbplib
changeset 14:a66aefd5e6ac
Added functions for converting vectors to and from cell format.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 22 Sep 2015 09:46:20 +0200 |
parents | b18d3d201a71 |
children | 16bad7c459da |
files | cell2vector.m vector2cell.m |
diffstat | 2 files changed, 28 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cell2vector.m Tue Sep 22 09:46:20 2015 +0200 @@ -0,0 +1,14 @@ +% cell2vector accepts a column cell array of column vectors and returns a columnvector +% with the input concatenated. It also returns the number of elements in each vector. +% cv -- column cell array with column vectors +% v -- vector of the concatenated vectors +% n -- number of elements in each vector before concatenation. Can be used with vector2cell(). +function [v, n] = cell2vector(cv) + v = []; + n = zeros(length(cv),1); + + for i = 1:length(cv) + n(i) = length(cv{i}); + v = [v; cv{i}]; + end +end \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vector2cell.m Tue Sep 22 09:46:20 2015 +0200 @@ -0,0 +1,14 @@ +% Splits column vector v into segments of length n and returns the result as a column cell array. +% v -- column vector to be split +% n -- number of elements in each part +% +% cv -- cell array of vectors with lenght n(i) +function cv = vector2cell(v,n) + cv = cell(length(n),1); + + ind = [0; cumsum(n)]; + for i = 1:length(n) + ind_i = (ind(i)+1):ind(i+1); + cv{i} = v(ind_i); + end +end \ No newline at end of file