Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

matrix - Is MATLAB row specific or column major?

In MATLAB, we can operate on both rows and columns of a matrix. What does it exactly mean by "row major" or "column major"?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is important to understand that MATLAB stores data in column-major order, so you know what happens when you apply the colon operator without any commas:

>> M = magic(3)
M =
     8     1     6
     3     5     7
     4     9     2
>> M(:)
ans =
     8
     3
     4
     1
     5
     9
     6
     7
     2

I tend to think "MATLAB goes down, then across". This makes it easy to reshape and permute arrays without scrambling your data. It's also necessary in order to grasp linear indexing (e.g. M(4)).

For example, a common way to obtain a column vector inline from some expression that generates an array is:

reshape(<array expression>,[],1)

As with (:) this stacks all the columns on top of each other into single column vector, for all data in any higher dimensions. But this nifty syntactic trick lets you avoid an extra line of code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...