view +blockmatrix/toMatrix.m @ 577:e45c9b56d50d feature/grids

Add an Empty grid class The need turned up for the flexural code when we may or may not have a grid for the open water and want to plot that solution. In case there is no open water we need an empty grid to plot the empty gridfunction against to avoid errors.
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 07 Sep 2017 09:16:12 +0200
parents 46aa0b6a10cd
children c14875cf7ae6
line wrap: on
line source

function A = toMatrix(bm)
    if ~blockmatrix.isBlockmatrix(bm)
        error('blockmatrix:toMatrix:NotABlockmatrix', 'Input is not a blockmatrix');
    end

    div = blockmatrix.getDivision(bm);
    n = div{1};
    m = div{2};

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

    A = sparse(N,M);

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

    for i = 1:size(bm,1)
        for j = 1:size(bm,2)
            if isempty(bm{i,j})
                continue
            end
            % TODO: If this ever fails for large matrices. Try cell2mat instead.
            A(n_ind(i)+1:n_ind(i+1),m_ind(j)+1:m_ind(j+1)) = bm{i,j};
        end
    end
end