MATLAB submatrix over variable dimensions

Posted by rlbond on Stack Overflow See other posts from Stack Overflow or by rlbond
Published on 2010-05-28T18:27:13Z Indexed on 2010/05/28 18:42 UTC
Read the original article Hit count: 242

Filed under:

I have a variable dimension matrix, X. I want a function that will get the first half of X in one dimension. I.E., I want something like this:

function x = variableSubmatrix(x, d)
    if d == 1
        switch ndims(x)
            case 1
                x = x(1:end/2);
            case 2
                x = x(1:end/2, :);
            case 3
                x = x(1:end/2, :, :);
            (...)
        end
    elseif d == 2
        switch ndims(x)
            case 2
                x = x(:, 1:end/2);
            case 3
                x = x(:, 1:end/2, :);
            (...)
        end
    elseif (...)
    end
end

I'm not quite sure how to do this. I need it to be fast, as this will be used many times in computation.

© Stack Overflow or respective owner

Related posts about matlab