view cell2sparse.m @ 87:0a29a60e0b21

In Curve: Rearranged for speed. arc_length_fun is now a property of Curve. If it is not supplied, it is computed via the derivative and spline fitting. Switching to the arc length parameterization is much faster now. The new stuff can be tested with testArcLength.m (which should be deleted after that).
author Martin Almquist <martin.almquist@it.uu.se>
date Sun, 29 Nov 2015 22:23:09 +0100
parents a5fa3c2d0aa3
children
line wrap: on
line source

function A = cell2sparse(C)

    if isempty(C)
        A = sparse([]);
        return
    end

    n = row_height(C);
    m = col_width(C);

    N = sum(n);
    M = sum(m);

    A = sparse(N,M);

    n_ind = [0 cumsum(n)];
    m_ind = [0 cumsum(m)];

    for i = 1:size(C,1)
        for j = 1:size(C,2)
            if ~has_matrix(C{i,j})
                continue
            end
            A(n_ind(i)+1:n_ind(i+1),m_ind(j)+1:m_ind(j+1)) = C{i,j};
        end
    end

end

function m = col_width(C)
    for j = 1:size(C,2)
        for i = 1:size(C,1)
            if ~has_matrix(C{i,j})
                continue
            end
            m(j) = size(C{i,j},2);
        end
    end
end

function n = row_height(C)
    for i = 1:size(C,1)
        for j = 1:size(C,2)
            if ~has_matrix(C{i,j})
                continue
            end
            n(i) = size(C{i,j},1);
        end
    end
end

function b = has_matrix(c)
    b = ~(isempty(c) || (numel(c)==1 && c == 0));
end