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

  • Context: MATLAB 
  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    matlab
Click For Summary
SUMMARY

The discussion focuses on the behavior of matrix indexing in MATLAB, specifically the command B([1 2], [1 3]) applied to the matrix B = [0 5 1; 2 4 3]. This command extracts the first and second rows and the first and third columns, resulting in the matrix [[0, 1], [2, 3]]. The user clarifies that the notation allows for flexible selection of rows and columns in any order, as demonstrated by the example B([2 1], [3 2]), which yields a different arrangement of elements. The discussion emphasizes the importance of understanding MATLAB's matrix indexing for effective data manipulation.

PREREQUISITES
  • Familiarity with MATLAB syntax and commands
  • Understanding of matrix structures and indexing
  • Basic knowledge of linear algebra concepts
  • Experience with data manipulation in programming environments
NEXT STEPS
  • Explore MATLAB matrix indexing techniques in detail
  • Learn about advanced MATLAB data manipulation functions
  • Study the implications of row and column selection on matrix operations
  • Review MATLAB documentation on matrix operations and examples
USEFUL FOR

Students, educators, and professionals working with MATLAB who need to understand matrix indexing for data analysis and manipulation tasks.

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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K