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

后端 未结 2 792
抹茶落季
抹茶落季 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.

提交回复
热议问题