Why Matlab indexing for Matrices is not the same as that of Vectors?

Hi, I wonder why indexing is setup differently for matrices versus vectors ? If A is a matrix, A(B) has the same size of B. But if A is vector and B also a vector, A(B) will be a column or row vector depending on A and not on B. As an example:
>A=magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
> A([1:5]')
ans =
16
5
9
4
2
> A([1:5])
ans =
16 5 9 4 2
> A=ones(1,10);
> A([1:5])
ans =
1 1 1 1 1
> A([1:5]')
ans =
1 1 1 1 1
As you see , if A is a matrix, A([1:5]) is a row vector and A([1:5]') is a column vector. But if A is a row vector, A([1:5]) and A([1:5]') have the same size and both a row vector.
This double standard in treating a Matrix and a Vector produces special cases in the code that you have to correct to avoid error messages.
Why?
-Arash

6 Comments

Btw.: You do not need square brackets around a vector: "[1:5]" is slower than "1:5".
is it slower - its not for me (Windows R2009b) - is this true for newer releases?
In theory the overhead of the call to horzcat() slows it down. I'm not able to reproduce this on R2011b 64bit, though:
t1 = 0;
t2 = 0;
for ii = 1:5000000
tic
x = 1:2;
t1 = t1+toc;
tic
y = [1:2];
t2 = t2+toc;
end
disp([t1 t2]);
5.6346 5.5493
I have heard before that it slows it down - but never been able to reproduce it either -
your test on my PC produces: 3.2038 3.2034
Four 10000ths of a second difference!! Not too shabby after five million iterations.

Sign in to comment.

 Accepted Answer

docsearch linear indexing
Gives a good set of explanations. The only time a row vector is returned from a linear index column vector (as in your example above) is when A is a row vector

5 Comments

Yes Sean, I understand that. I am asking WHY Matlab chose to do it this way?
They say : "If you index into a vector with another vector, the orientation of the indexed vector is honored for the output. If you index into a vector (or matrix) with a nonvector, the shape of the indices is honored. "
This to me is a flawed design. A vector is a special case of a matrix. If in your code you arrange your data into a matrix such that columns represent time and rows represent different samples, with several samples you get a matrix of data, and with one sample you get a vector of data. WHY on earth should you write two different lines of codes to treat their indexing differently?
I want to know why Mathwork chose to do it this way? Were they just sloppy or they tried to accomplish something else?
Thanks for your attention !
Arash
MATLAB's general design is to treat row vectors and column vectors the same way in functions. It occurs in many different functions, and it saves people from people having to worry about row or column vectors for common operations.
When you are designing a tool, sometimes the most usable design is the one that gets the job done for the most people, rather than the ideologically pure design.
@Walter. Yup, it makes sense, even though down the road it creates some hassle.
It can be a hassle occasionally (especially when find() returns a column vector). I usually just force everything to column vectors using the colon operator (x(:)) if there's any quuestion.

Sign in to comment.

More Answers (0)

Categories

Products

Tags

Community Treasure Hunt

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

Start Hunting!