作业帮 > 综合 > 作业

matlab中如何任意选取一个矩阵的任意几行几列

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/09 18:14:09
matlab中如何任意选取一个矩阵的任意几行几列
a =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> b=a(3,:) %第三行
b =
4 6 13 20 22
>> c=a(:,3) %第三列
c =
1
7
13
19
25
选取其他行列方法类似
再问: 请问如果选取的行和列都是不定的,并且选几行几列也是任意的,不一定是一列或一行,要如何实现呢
再答: 用随机数作为index
clc;clear;
mtx=magic(5);
a=randperm(5); %产生随机行、列数目
index=a(1:2);
temp1=randperm(5);
row=sort(temp1(1:a(1)))
temp2=sort(randperm(5));
col=sort(temp2(1:a(2)))
m1=mtx(row,:)%按行组合
m2=mtx(:,col)%按列组合