MATLAB: extracting groups of columns into a submatrix?

RasmusHoell picture RasmusHoell · Apr 13, 2012 · Viewed 15.1k times · Source

I have a data-set, in which I want to extract columns 1-3, 7-9, 13-15, all the way to the end of the matrix

As an example, I've used the standard magic function to create a matrix

A=magic(10)

A =

92    99     1     8    15    67    74    51    58    40
98    80     7    14    16    73    55    57    64    41
 4    81    88    20    22    54    56    63    70    47
85    87    19    21     3    60    62    69    71    28
86    93    25     2     9    61    68    75    52    34
17    24    76    83    90    42    49    26    33    65
23     5    82    89    91    48    30    32    39    66
79     6    13    95    97    29    31    38    45    72
10    12    94    96    78    35    37    44    46    53
11    18   100    77    84    36    43    50    27    59

I know that I can extract single columns starting at 1, in intervals of 3 with the command:

Aex=a(:,1 : 3 : end)

Aex =

92     8    74    40
98    14    55    41
 4    20    56    47
85    21    62    28
86     2    68    34
17    83    49    65
23    89    30    66
79    95    31    72
10    96    37    53
11    77    43    59

Say I want to extract groups of columns instead (e.g. column 1-3, 7-9 etc.).

Is there a way to do this without having to manually point out all the column numbers?

Thanks for your help! Rasmus

Answer

High Performance Mark picture High Performance Mark · Apr 13, 2012

Is this what you are looking for:

 Aex = A(:,[1:3 7:9])

?