only keep selected indexes of array and discard rest in matlab

To only keep selected indexes of an array in MATLAB, you can use indexing to create a subset of the original array.

Here's an example:

main.m
% Define an example array
A = [7, 4, 2, 9, 1, 8];

% Define the indices to keep
indices_to_keep = [1, 3, 5];

% Use indexing to create a subset of the original array with only the selected indices
subset = A(indices_to_keep);
226 chars
9 lines

In this example, indices_to_keep contains the indices that we want to keep from the original array A. We then create a new variable subset that only contains the elements of A with those indices.

The resulting subset array would be:

main.m
subset =

     7     2     1
29 chars
4 lines

Note that this method can also be used for multidimensional arrays by specifying a subset of indices for each dimension.

gistlibby LogSnag