Matlab Concatenation Help: Resolving B([1 2], [1 3])

  • Context: MATLAB 
  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 4K views
gfd43tg
Gold Member
Messages
949
Reaction score
48
Hello,

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

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

B =

     0     5     1
     2     4     3

Code:
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.
 
Physics news on Phys.org
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
 
  • Like
Likes   Reactions: 1 person
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: