老师给的作业 让我们把一个随机矩阵中,平方和最小的行和列找出来,并移到第一行第一列。找出来我已经找出来了,平移还有问题,我是想用提出找到的行列并赋值给某个元素,然后再把这个元素用A=[a;A]这种把它合成回去,但出问题了。
% input:
% A arbitrary real matrix
% output:
% nrows number of rows
% ncols number of columns
% imin index of row with smallest sum of squares
% jmin index of col with smallest sum of squares
% B A with row/col imin/jmin moved to 1st row/col
A = randi(20,[7,6])-10
[nrows,ncols] = size(A);
m=nrows;n=ncols;
x=A(1,:);y=A(:,1);C=zeros(7,6);D=A';imin=1;jmin=1;
for i=1:m;
a=norm(A(i,:));a=a^2;
c=norm(x);c=c^2;
if c>a;
x=A(i,:);
imin=i;
end
end
for i=1:n;
b=norm(A(:,i));b=b^2;
d=norm(y);d=d^2;
if d>b;
y=A(:,i);
jmin=i;
end
end
p=A(imin,:);q=A(:,jmin);
if 7>imin>1;
C=[A(1:(imin-1),:);A((imin+1):m,:)];
C=[p;C];
else if imin>6;
C=[A(1:6)];
C=[p;C];
end
end
if 7>jmin>1;
C(jmin,:)=[];
C=[q,C];
end
B=C;
但输出结果却是这种东西……
A =
-5 -6 9 -4 -1 5
-3 -5 10 3 1 0
0 -1 -1 5 -8 2
-5 -3 -7 -5 -4 -5
7 9 -4 -7 7 0
-6 -1 -1 -4 -9 10
-5 -6 2 -3 9 1
m =
7
n =
6
irow =
3
jcol =
4
B =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
为什么B会变成0矩阵??!!
(顺便说一句,老师给的提示是弄个C=zeros(7,6)之类的然后行、列、元素赋值,但我不知道怎么搞所以想了个这么个办法……新手轻喷求指导)