MATLAB: Array Indexing, Array within an Array

arrayindexindexingMATLABmatricesmatrixmatrix arrayvariable

I need help understanding how this code works.
Consider the variables B = [1 5 8] and D = [1; 1; 1]. When I write the code B(D,:) I am getting the output of [1 5 8; 1 5 8; 1 5 8] and I am not sure why.
Please be thorough in your explination.
Thanks for the help.

Best Answer

Because, by definition,
B(D,:) = [ B(D(1),:) ; B(D(2),:) ; B(D(3),:) ]
= [ B(1,:) ; B(1,:) ; B(1,:) ]
= [1 5 8; 1 5 8; 1 5 8]