MATLAB: How does MATLAB ‘fill in’ elements when subsetting a matrix by vectors

subsetting

A = [1, 2, 3;
4, 5, 6;
7, 8, 9];
B = A([end, 1:end], [end, 1:end])
% B =
%
% 9 7 8 9

% 3 1 2 3
% 6 4 5 6
% 9 7 8 9
Why is the first element of matrix B, i.e. B(1,1), 9?
All the other elements seem to follow the 'classic' rules of concatination/subsetting but how does MATLAB 'fill in' element (1,1)?

Best Answer

Because A(end,end) = 9.
EDIT —
This is a bit more obvious analyzing how MATLAB indexes matrices.
The linear index for ‘A’ is:
LinA =
1 2 3 4 5 6 7 8 9
1 4 7 2 5 8 3 6 9
with the linear index on the first row and the corresponding element in the second row.
The linear indices as you call them (and MATLAB then calculates them) are:
LinIdx =
9 7 8 9 3 1 2 3 6 4 5 6
and the corresponding elements of ‘A’ are:
LinB =
9 7 8 9 3 1 2 3 6 4 5 6
9 3 6 9 7 1 4 7 8 2 5 8
again with the linear index on the first row and the corresponding element in the second row.