haibaraxx的个人博客分享 http://blog.sciencenet.cn/u/haibaraxx

博文

Matlab: indexing

已有 2007 次阅读 2016-3-8 04:53 |个人分类:Matlab|系统分类:科研笔记| indexing

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]



https://blog.sciencenet.cn/blog-3031432-961143.html

上一篇:Matlab: operations for matrix
下一篇:Matlab: list variables in the workspace
收藏 IP: 134.1.1.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-7-17 19:38

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部