Need help understanding arrays

2 visualizzazioni (ultimi 30 giorni)
Joe
Joe il 12 Mar 2013
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.

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 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

Più risposte (1)

Cedric
Cedric il 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.

Community Treasure Hunt

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

Start Hunting!

Translated by