Need help understanding arrays
1 view (last 30 days)
Show older comments
Let's say I have an array A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6
A(1:2) would simply be
1
4
And A(2:3) would be
4
2
Basically the the 1st and 2nd elements and the 2nd and 3rd elements.
So why then is A(1:2, 2:3)
2 3
5 6
Instead of
1 2
4 5
This is probably a silly question, but I just can't seem to understand this.
0 Comments
Accepted Answer
Andrei Bobrov
on 12 Mar 2013
for your case:
A ([1:2,2:3])
for:
A(1:2,2:3);% 1:2 - numbers rows, befor the comma
% 2:3 - numbers columns, after the comma
0 Comments
More Answers (1)
Cedric
on 12 Mar 2013
It is not a silly question. Your first approach for indexing is called 'linear indexing'; it addresses A as if it were a column vector made of all columns of A appended. Try
>> A(:)
and you will understand. The second approach is subscript indexing of a block of A made of rows 1 to 2 and columns 2 to 3.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!