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
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.
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_new
is now 30x20
You can also iterate over elements in two loops, but the above answer is easiest