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

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    matlab
AI Thread Summary
The discussion centers on understanding matrix indexing in MATLAB, specifically how to extract elements from a matrix using row and column indices. The matrix B is defined as a 2x3 matrix, and the command B([1 2], [1 3]) is analyzed. This command selects the first and second rows and the first and third columns, resulting in a new 2x2 matrix. The confusion arises from the expectation that the column indices would directly correspond to the original matrix's columns, but they actually specify which columns to include in the output. The discussion clarifies that the order of selection for rows and columns can be reversed, as demonstrated with the example B([2 1], [3 2]), which produces a different arrangement of elements. The importance of understanding this indexing method is emphasized, particularly for academic purposes.
gfd43tg
Gold Member
Messages
947
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 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:

Similar threads

Back
Top