how to assign part of a matrix to other matrix in matlab

后端 未结 2 787
抹茶落季
抹茶落季 2021-01-29 12:41

I have a 30 x 30 matrix, called A, and I want to assign B as the leftmost 30 x 20 block of A how can I do that?

Is this the correct way to do i

相关标签:
2条回答
  • 2021-01-29 12:51

    No the correct way is

    B = A(:, 1:20);
    

    where : is shorthand for all of the rows in A.

    Matrix indexing in MATLAB uses round brackets, (). Square brackets, [], are used to declare matrices (or vectors) as in

    >> v = [1 2 3; 4 5 6; 7 8 9]
    v =
    
         1     2     3
         4     5     6
         7     8     9
    

    excaza provides a very good link on Matrix Indexing in MATLAB which should help you. There is also Matrix Indexing.

    0 讨论(0)
  • 2021-01-29 12:51

    A_new = A(:,1:20)

    takes all the rows from A with this part A(:,) and the first 20 columns with this part A(,1:20)

    A_newis now 30x20

    You can also iterate over elements in two loops, but the above answer is easiest

    0 讨论(0)
提交回复
热议问题