difference between data(:,3:4) and data(:,end:end-1) in data=7x4 matrix

조회 수: 2 (최근 30일)
민주
민주 2023년 8월 25일
답변: Walter Roberson 2023년 8월 25일
what is the difference between them?
I'm learning MATLAB Onramp tutorial section and occording to the course,
data(:,3:4) indicates 7x2 and data(:,end:end-1) indicates 7x0.
isn't :end-1 and :4 same thing in the given matrix? what are the differences between two?

답변 (2개)

Steven Lord
Steven Lord 2023년 8월 25일
Order matters. I'm going to make the second number a little bigger to more clearly illustrate what's going on.
v1 = 3:5
v1 = 1×3
3 4 5
v2 = 5:3
v2 = 1×0 empty double row vector
When you use the two-input form of the colon operator, you're trying to get from the first number to the second number in steps of +1 unit.
For v1, can you get from 3 to 5 in steps of +1 unit? Yes, and it takes two steps: 3 to 4 and 4 to 5.
For v2, can you get from 5 to 3 in steps of +1 unit? No. You could get from 5 to 3 in steps of -1 unit, but that's not what the two-input form computes. For that you'd need the three-input form.
v3 = 5:-1:3
v3 = 1×3
5 4 3
For your matrix, the way you're indexing into it you're asking to extract the rows from 4 to 3. Can you get there in steps of +1 unit? No, so you get zero rows of the matrix you're indexing into.

Walter Roberson
Walter Roberson 2023년 8월 25일
For a 7 x 4 matrix, end as the second coordinate would be 4, and end-1 for the second coordinate would be 3. So data(:,end:end-1) would be data(:,4:3) . But when you use the : operator without an increment, the default increment is +1, as if you had written 4:1:3 . And when you use a positive increment and the last value (3) is less than the first value (4) then the result is empty.
If you had used data(:, end-1:end) then that would be the same as data(:,3:4)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by