Get even/odd indices of a matrix - MATLAB

25,648

Solution 1

It's pretty easy to do with indexing:

Odd rows and odd columns

B = A(1:2:end, 1:2:end);

Odd rows and even columns

B = A(1:2:end, 2:2:end);

Even rows and odd columns

B = A(2:2:end, 1:2:end);

Even rows and even columns

B = A(2:2:end, 2:2:end);

The above assumes that you want the actual matrix values themselves. It's a bit confusing as your matrix elements are the same as linear indexing values themselves. If you want to determine the actual column major indices to access the matrix, you can generate a vector from 1 to N where N is the total number of elements in your matrix, then reshape this matrix into the desired size that you want. After, use the same logic above to get the actual linear indices:

N = numel(A);
B = reshape(1:N, size(A,1), size(A,2));
ind = B(1:2:end, 1:2:end); %// For odd rows, odd columns
%// Repeat for the other ones...

Now, given your comment, you want to create a new matrix that will store only these extracted matrix values while making all of the other elements zero. If you want to do this, simply pre-allocate a matrix of zeroes, then copy over those values to extract using the computed indices into the new matrix. In other words:

N = numel(A);
B = reshape(1:N, size(A,1), size(A,2));
ind = B(1:2:end, 1:2:end); %// For odd rows, odd columns - Change to suit your tastes
out = zeros(size(A));
out(ind(:)) = A(ind(:));

If you want to combine the indices like having odd row - odd column, and even row - even column, just compute two sets of indices, concatenate them into a single vector and do the same syntax like before. Therefore:

N = numel(A);
B = reshape(1:N, size(A,1), size(A,2));
ind = B(1:2:end, 1:2:end); %// For odd rows, odd columns
ind2 = B(2:2:end, 2:2:end); %// For even rows, even columns
ind = [ind(:); ind2(:)];
out = zeros(size(A));
out(ind) = A(ind);

Solution 2

Code

N = size(A,1); %// Get size of input matrix A

case1_ind = bsxfun(@plus,[1:2:N]',(0:N/2-1)*2*N)
case2_ind = case1_ind + N
case3_ind = case1_ind + 1
case4_ind = case3_ind + N

Note: These outputs are indices. So, to get the actual outputs, use these as indices.

To combine indices for case 1 and case 4, just concatenate -

case14comb_ind = [case1_ind ; case4_ind]

Edit :

%// To copy onto some other matrix of the same size as A, do this for case 1
new_matrix = zeros(size(A))
new_matrix(case1_ind(:)) = A(case1_ind(:))

Repeat this for the other cases too.

Share:
25,648
DrSkyer
Author by

DrSkyer

Updated on October 28, 2020

Comments

  • DrSkyer
    DrSkyer over 3 years

    I have following problem:

    I have a given matrix of let's say 4x4. How can I get the indices of the following combinations:

    • row odd and column odd
    • row odd and column even
    • row even and column odd
    • row even and column even

    For example if I have the matrix:

    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
    
    • 'row odd and column odd' would be the indices of 1, 3, 9, 11...
    • 'row odd and column even' would be the indices of 2, 4, 10, 12...
    • 'row even and column odd' would be the indices of 5, 7, 13, 15...
    • and 'row even and column even' would be indices of 6, 8, 14, 16...

    Also, is it possible to combine those operations so e.g. I get the indices for 'row odd and column odd' and 'row even and column even'?

    Thank you!