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

2 次查看(过去 30 天)
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