简体   繁体   中英

Octave access elements of matrix by lines

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 !

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. ie 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:

转置(A)

So now as per the column major order, first, second and third elements are -1 , 1 and -2 respectively and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM