Asked by ArashMat Fazl
on 20 Dec 2011

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

Answer by Sean de Wolski
on 20 Dec 2011

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

Show 2 older comments

Naz
on 21 Dec 2011

@Walter. Good point

ArashMat Fazl
on 21 Dec 2011

@Walter. Yup, it makes sense, even though down the road it creates some hassle.

Sean de Wolski
on 21 Dec 2011

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.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 6 Comments

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/24413#comment_53718

Btw.: You do not need square brackets around a vector: "[1:5]" is slower than "1:5".

## Robert Cumming (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/24413#comment_53970

is it slower - its not for me (Windows R2009b) - is this true for newer releases?

## Sean de Wolski (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/24413#comment_53974

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

## Robert Cumming (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/24413#comment_53976

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

## Sean de Wolski (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/24413#comment_53978

Four 10000ths of a second difference!! Not too shabby after five million iterations.

## Robert Cumming (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/24413#comment_53981

indeed :)