function v = mat2vec(M)
% Convert matrix M to vector v columnwise

[nr,nc]= size(M);
v= zeros(nr*nc,1);

for c= 1:nc
    v((c-1)*nr+1:c*nr)= M(:,c);
end