Array Indexing, Array within an Array

3 views (last 30 days)
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.

Accepted Answer

Matt J
Matt J on 19 Jun 2019
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]

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!