Is there a reason why row vectors are default in Matlab?
Show older comments
Why is the default vector a row vector in Matlab? I do not have problems coding, but I wonder if anybody knows why Matlab does this.
When I create a vector like 1:100, or loop variablename(i), Matlab will create a row vector i.e. add element in the second dimension. In essence, variable(i) == variable(1,i) when creating vectors. This is illogical and cumbersome for the following reasons:
- When I create vectors and view them in Matlab, having them in the first dimension makes it easy to scroll through the values. I can't scroll sideways.
- matrix indexation should be logical. If I give one index, it's only logical to index the first dimension i.e. create a column vector.
- taking length(size(vector)) gives the number of dimensions of a matrix, unless of course, for vectors, which are created with an added singleton-dimension.
- Conversely, converting a matrix into a vector with matrix(:) creates a column vector, but why not create all vectors in the first dimension by default?
Granted, it is easy to bypass, often by transposing [vector]' or generating it in loop using vector(i,1). I'm just curious if there is a good reason for it
1 Comment
For those interested in this topic, also see discussion here:
Accepted Answer
More Answers (4)
Both the "A History of MATLAB" article that Cleve Moler and Jack Little wrote that was published by the ACM (see this blog post) as well as the Historic MATLAB Users' Guide (this other blog post) mention that the colon, : operator produce a row vector, but not why it does. So I guess the real answer is:
why(189)
Jan
on 29 Jun 2022
0 votes
Matlab was developped as "Matrix Laboratory". So the default type is a double matrix. If the focus has been set to vectors, it would have the name "Veclab".
This is implemented consistently, e.g. length(size(1)) is 2 also, wich is a [1 x 1] matrix.
The automatic vanishing of trailing singelton dimensions, except if it is 2nd one, might feel strange, but this has been the decision some decades ago.
I'm not sure, if this decision has a "good" reason.
1 Comment
Jonatan Tidare
on 30 Jun 2022
Bruno Luong
on 2 Jul 2022
Edited: Bruno Luong
on 2 Jul 2022
Beside an historical reason to me it seems it related to for the for-loop is designed to work
For M a true matrix
for v = M
...
end
it will loop on column of M, meaning v is M(:,1) at the first iteration, ... M(:,end) the last. Which is desirable since v is a column vector, compatible with vector in linear algebra and also it is a contiguous in memory due to MATLAB major-column memory storage, so more efficient for the for-loop to run.
Therefore a row vector is desired so that when you do
for x = r
...
end
it is desired to have r as (1 x n) in size so that it can really loop n times. Therefore the default value of expression (a:b) is to create a vector as row vector, ready to be looped and can be insert directly to a loop index as following:
for x = a:b
...
end
This is my opinion, what the creators of MATLAB was thinking I have no idea.
If you change one of the above organization, the for-loop syntax will be much messy or not efficient.
NOTE that for long time MATLAB did not have nd-array.
1 Comment
Adam Danz
on 2 Jul 2022
Edited: Walter Roberson
on 2 Jul 2022
See relevant discussion here
particularly Steven Lord's reference to an old discussion on comp.soft-sys.matlab.
Jonatan Tidare
on 7 Jul 2022
0 votes
1 Comment
Bruno Luong
on 7 Jul 2022
If you are still curious about for loop
for v = A
...
end
Guess what is v and how many time it loops if A is nd-array (m1 x m2 x ... mn). I don't think I ever use this feature.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!