Reputation: 2096
I made a matrix like this :
A = randi([-10, 10], 3, 3);
Which can have this result :
-1 1 -2
2 2 8
5 3 10
How can I transform it in a way that A(1) = -1, A(2) = 1 and A(3) = -2 (Accessing first line with terms 1,2,3)
Currently, A(1) = -1, A(2) = 2 and A(3) = 5 (columns)
Note : Not only the first line, but I want to access all elements sorted by lines
Thank you !
Upvotes: 0
Views: 673
Reputation: 19689
In Octave and MATLAB, data is stored in column-major order which means for your matrix, indices and elements are like this:
You need to take the transpose of the original matrix to access them the way you stated. In Octave, you can directly access them using A.'(1)
, A.'(2)
, A.'(3)
etc. In MATLAB, you cannot access them like Octave. So save them in a new matrix or replace the contents of the previous matrix. i.e. A = A.'
and then you can use A(1)
, A(2)
, A(3)
etc to access the desired elements.
A.'
or transpose(A)
for the given A
actually gives:
So now as per the column major order, first, second and third elements are -1
, 1
and -2
respectively and so on.
Upvotes: 3