difference between data(:,3:4) and data(:,end:end-1) in data=7x4 matrix
4 views (last 30 days)
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?
Steven Lord on 25 Aug 2023
Order matters. I'm going to make the second number a little bigger to more clearly illustrate what's going on.
v1 = 3:5
v2 = 5:3
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
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 on 25 Aug 2023
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)