how does the vector extration operated in the following commands

1 view (last 30 days)
Fan Yang
Fan Yang on 4 Aug 2018
Commented: Fan Yang on 4 Aug 2018
a=[3 1 2 12 4]
why does x contain 1, 2, 12, and 4?

Accepted Answer

Walter Roberson
Walter Roberson on 4 Aug 2018
If you meant x=a(2:end) then that would contain 1, 2, 12, and 4.
When used in a single index, "end" stands in for numel() of the array it is being applied to. Your a is length 5, so a(2:end) stands in for a(2:numel(a)) which is a(2:5) . So the second, third, fourth, and fifth elements of a would be selected.
If you were using two dimensional indexing, like
b = [3 1 2 12 14; -4 9 3 8 2]
then "end" stands in for size() of the index in that position. So b(:,2:end) would stand in for b(:, 2:size(b,2)) which would be b(:, 2:5) . The : in the first position would stand in for 1:end, as if you had written b(1:end, 2:end), so that would be b(1:size(b,1), 2:size(b,2)) which would be b(1:2, 2:5) and would give columns 2, 3, 4, 5 of both rows of b.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!