フィルターのクリア

how does the vector extration operated in the following commands

1 回表示 (過去 30 日間)
Fan Yang
Fan Yang 2018 年 8 月 4 日
コメント済み: Fan Yang 2018 年 8 月 4 日
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.
  1 件のコメント
Fan Yang
Fan Yang 2018 年 8 月 4 日
I did mean to say x=a(2:end), and I appreciate your answer. Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by