matlab 代码 从矩阵中随机选取两列,然后交换两列 在MATLAB中如何实现在一个矩阵中随机选取一行
Matlab\u4e2d\u5982\u4f55\u5c06\u4ece\u77e9\u9635\u4e2d\u968f\u673a\u9009\u53d6n\u884cn\u5217\u7684\u65b0\u77e9\u9635\u7136\u540e\u8fdb\u884c\u91cd\u65b0\u6392\u5217\u5047\u8bbe\u884c\u6570\u6216\u5217\u6570\u4e3an
vector = randperm\uff08n\uff09;
vector\u662f\u4ece1\u5230n\u7684\u6574\u6570\u968f\u673a\u6392\u5217\u7684\u5411\u91cf
\u6309\u7167vector\u91cc\u5404\u4e2a\u5143\u7d20\u7684\u503c\u91cd\u65b0\u6392\u5217\u884c\u5217\u5411\u91cf\u5c31\u53ef\u4ee5\u4e86
\u5047\u8bbe\u5f85\u6392\u5217\u77e9\u9635\u4e3aM\uff0c\u6392\u5217\u540e\u7684\u77e9\u9635\u4e3aA
\u6309\u5217\u968f\u673a\u6392\u5217
A = zeros(size(M));
for i=1:n
A(:,i)=M(:,vector(i));
end
n=randi(size(a,1));
b=a(n,:);
A = randi(10,5,7)
cc = size(A,2);
id = randperm(cc);
id = id(1:2);
tm = A(:,id(1));
A(:,id(1)) = A(:,id(2));
A(:,id(2)) = tm
disp(['交换的列: ' num2str(id)]);
A =
2 1 8 10 7 1 3
3 3 5 6 7 9 4
4 9 6 6 4 10 7
5 1 3 3 4 8 2
6 10 5 5 10 1 8
A =
8 1 2 10 7 1 3
5 3 3 6 7 9 4
6 9 4 6 4 10 7
3 1 5 3 4 8 2
5 10 6 5 10 1 8
交换的列: 1 3
扩展阅读:matlab代码生成器 ... matlab table转为矩阵 ... matlab怎样初始化场的矩阵 ... matlab把数据变成对角矩阵 ... matlab转置矩阵代码 ... matlab用代码画矩阵图 ... matlab代码自动翻译 ... matlab代码大全 ... matlab 矩阵 四舍六入 ...