|||
A = [1 2 3; 4 5 6];
B = A'; % transposed A, i.e. [1 4; 2 5; 3 6]
% index the element in Row 2 and Column 3.
a_01 = A(2,3);
-> 6
% index the element in Row 2 and Column 1&3.
a_02 = A(2,[1 3]);
-> [4 6]
% index the elements in the last column and in Row 2&1.
a_03 =A([2 1], end);
-> [6; 3]
% index the elements in Row 1 and 2 of B with the correponding columns from 1 till the end.
b_01 = B(1:2,1:end);
-> [1 4; 2 5]
% index the element in the last but one row and last but one column.
b_02 =B(end-1,end-1);
-> 2
% index with increments
b_03 = B(1:2:3, 3:-2:1); % same as B([1 3], [3,1])
->[1 6; 3 4]
% create a new matrix by indexing
C(2,2)=2; % C is not defined before!
-> C is [0 0; 0 2]
% add rows and columns by indexing
A(3,4)=9;
-> A is [1 2 3 0; 4 5 6 0; 0 0 0 9]
A(end+1,4)=8;
-> A is [1 2 3 0; 4 5 6 0; 0 0 0 9; 0 0 0 8]
% make all the elements in Row 1 of A 8.
A(1,:)=8;
-> [8 8 8; 4 5 6]
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-7-17 19:38
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社