Reputation: 63656
Given the following example:
>> I=[2 1 3;3 2 4]
I =
2 1 3
3 2 4
>> I(:)
ans =
2
3
1
2
3
4
>> I(1:2)
ans =
2 3
Why does I(:)
return a column vector while I(1:2)
returns a shorter row vector?
Upvotes: 2
Views: 2546
Reputation: 125864
The (:)
syntax, when used as an index on the right-hand side of an equation, is a special operation that reshapes an entire matrix of any dimension into a single column vector. The following two lines of code therefore give the same result:
a = I(:); % Create a column vector with ":"
a = reshape(I, [], 1); % Create a column vector with RESHAPE
When numerical values are included on either side of a single colon, it denotes a range of linear indices into an array. So I(1:2)
selects the first and second elements from I
(i.e. the values in the first column). One thing to remember is that the syntax 1:2
actually creates a vector [1 2]
, so I(1:2)
is the same as I([1 2])
. Since the linear index [1 2]
is a row vector, the returned values are shaped as a row vector [2 3]
. If you use the index I([1; 2])
or I((1:2).')
, the linear index is a column vector, so the returned values will be shaped as a column vector [2; 3]
.
When you have multiple indices separated by commas, the indices are applied to different dimensions of the matrix being indexed. For example, I(1:2, 1:2)
will return the 2-by-2 matrix [2 1; 3 2]
. The first 1:2
in the index is applied to the rows, so rows one and two are selected. The second 1:2
in the index is applied to the columns, so columns one and two are selected.
The MATLAB documentation describing the colon operator and matrix indexing should help give you a better understanding of how to use :
effectively.
Upvotes: 9
Reputation: 95539
Examples of Matlab Indexing
[rows,cols] = size(M); % M is a rows x cols matrix
Accessing entry at row i, column j:
x = M(i,j);
Accessing all items on row i:
r = M(i,:);
Accessing all items on column j:
c = M(:,j);
Accessing entry at row i, column j, treating M as a vector:
x = M(rows*(j-1)+i);
Accessing the sub-matrix from row i to row j and from column p to column q:
S = M(i:j,p:q);
Accessing the entire matrix (redundant):
M = M(:,:);
Explanation
The colon operator either gives a range of indices (1:2 is the indices in range 1 to 2, inclusive, while 3:5 gives the range 3, 4, 5) or it gives the entire range for the given dimension if no range is specified.
This, in conjunction to the fact that indexing a matrix with just a single index gives you the entry that would result from stepping through that many entries (going down the rows, incrementing the column and resetting the row after the last row) instead of giving you just the specified row/column leads to your observations.
Upvotes: 2
Reputation: 34601
(:)
vectorizes a matrix along the columns, i.e. the elements are read along the columns are concatenated into a single column vector. a:b:c
returns a sequence of numbers from a
to c
with increments of b
. If b
is omitted, it is by default set to 1
.
The sequence a:b:c
can be used to index a matrix linearly along the column. If used to index a multidimensional array then it selects elements along that dimension. For e.g.
I(1,2:3)
returns a matrix formed by rows 1
and columns 2:3
of I
, i.e. [1 3]
Also, we can arrive at an index in any manner, and use it to index I.
index = [1 2 3];
disp(I(index));
The above displays the first three elements in column-major order (along the columns), i.e. [2 ; 3 ; 1]
Upvotes: 0