필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by