Get even / odd matrix indices - MATLAB

I have the following problem:

I have a given matrix, say 4x4

. How can I get the indices of the following combinations:

  • row is odd and column is odd
  • odd row and columns
  • line even and odd line
  • parity row and column

For example, if I have a matrix:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

      

  • 'row odd and column odd' will be indexes 1, 3, 9, 11 ...
  • 'row odd and column even' will be indexes 2, 4, 10, 12 ...
  • 'row even and column odd' will be indexes 5, 7, 13, 15 ...
  • and "row parity and columns equal" would be indices 6, 8, 14, 16 ...

Also, is it possible to combine these operations, for example I get the indexes for "odd row and odd columns" and "even rows and columns"?

Thank!

+3


source to share


2 answers


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 yourself want the matrix values ​​themselves. This is a little confusing since your matrix elements are the same as your linear indexing values. If you want to determine the actual primary column indices for accessing the matrix, you can generate a vector from 1 to N

, where N

is the total number of elements in your matrix, and then reformat that matrix to the size you want. After that, 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, based on your comment, you want to create a new matrix that will only store these extracted matrix values, making all other elements equal to zero. If you want to do this, just preallocate the matrix of zeros and then copy those values ​​to extract using computed indices into a 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 concatenate indices, for example with an odd column - an odd column and even a column with even rows, just compute the two sets of indices, concatenate them into one vector, and follow the same syntax as 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);

      

+11


source


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 them as indices.

To concatenate the 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 other cases as well.

+2


source







All Articles