# Matlab concatenation

Tags:
1. Jul 22, 2014

### Maylis

Hello,

I am curious why the following concatenation is not working the way I thought it would.

Code (Text):
B = [0 5 1; 2 4 3]

B =

0     5     1
2     4     3
Code (Text):
B([1 2], [1 3])

ans =

0     1
2     3
I don't really understand what this command is extracting from the matrix and how it is arranging it. It looks like B([1 2]) gets the 1st and 2nd element of B, but then the [1 3] part is not the 1st and 3rd element.

2. Jul 22, 2014

### AlephZero

The notation means B (a list of rows , a list of columns).

So if the original matrix was $\begin{matrix} b_{11} & b_{12} & b_{13} \\ b_{21} & b_{22} & b_{23} \end{matrix}$, you are selecting rows 1 and 2, and columns 1 and 3, which is
$\begin{matrix} b_{11} & b_{13} \\ b_{21} & b_{23} \end{matrix}$

Note you can select rows and columns in any order, so B([2 1], [3 2]) would give $\begin{matrix} b_{23} & b_{22} \\ b_{13} & b_{12} \end{matrix}$

There is an example with a picture here: http://www.mathworks.co.uk/company/newsletters/articles/matrix-indexing-in-matlab.html

3. Jul 22, 2014

### Maylis

Thanks, and great example. That is really tricky when you do everything backwards like that. I see how that one is, and I see the algorithm for how to insert into the matrix. I get conceptually now, but the algorithm can be a backup in case I have a blank out on the exam.

B([2 1], [3 2]) = $b_{23}$
B([2 1], [3 2]) = $b_{22}$
B([2 1], [3 2]) = $b_{13}$
B([2 1], [3 2]) = $b_{12}$

Last edited: Jul 22, 2014