iscell      = x -> type(x, "cube[??]")

% Function: cat
% Horizontal concatenation function: works for matrix, complex matrices and cell matrices
%
% : function y = cat(a, b)
%
function y = cat(a, b)
    assert(size(a,0) == size(b,0) && size(a,2) == size(b,2), _
            "Wrong dimensions for function 'cat'")
    %assert(type(a) == type(b), "'cat': arguments should be of the same type!")

    [m,n,k] = size(a,0..2)
    n2 = size(b,1)
    if iscell(a)
        y = cell(m,n+n2,k)
    else
        y = zeros(m,n+n2,k)
    endif
    y[:,0..n-1,:] = a
    y[:,n..n+n2-1,:] = b   
end


% Function: lincell
% Linearize a cell array, i.e. convert nested cell arrays
% to one 1D cell array.
%
% : function y = lincell(x)
%
% Notes:
% this function only involves pointer copies, which are very fast
function y = lincell(x)
    y = `'
    for m=0..size(x,0)-1
        for n=0..size(x,1)-1
            if iscell(x[m,n])
                y = cat(y, lincell(x[m,n]))
            else
                xcell = cell(1)
                xcell[0] = x[m,n]
                y = cat(y, xcell)
            endif
        end
    end
end

function [] = main()
    tic()
    for k=0..13000
    c = lincell(``1,2´,`3,`4´´´)
    end
    toc()
    %imshow([0,0])
end