how to extract last 2 columns of table in matlab

To extract the last 2 columns of a matrix in Matlab, you can use indexing. Here's an example code:

main.m
% create a sample matrix
mat = [1 2 3; 4 5 6; 7 8 9];

% extract last 2 columns
last_2_cols = mat(:, end-1:end);
% the ':' operator means "all rows", while 'end-1:end' means "last 2 columns"

% display the result
disp(last_2_cols)
231 chars
10 lines

This will output:

main.m
2     3
5     6
8     9
24 chars
4 lines

Note that the result is also a matrix, with the same number of rows as the original matrix but only the last 2 columns.

gistlibby LogSnag