how does the vector extration operated in the following commands

1 次查看(过去 30 天)
a=[3 1 2 12 4]
x=(2:end)
why does x contain 1, 2, 12, and 4?

采纳的回答

Walter Roberson
Walter Roberson 2018-8-4
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]
b(:,2:end)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by