回复: [求助]如何把一个包含0和1的矩阵中的相邻的1都加起来
说明:你把你的矩阵放在一个.txt文件中,保存在work文件夹下。这样比直接输入还要省事。
a=load('shuju.txt');
[index1 index2]=size(a);
% search the range of a
max=0;
for j=1:1:index1
count=0;
flag=1;
for i=1:1:index2
if (a(j,i)==1)&&(flag)
count=count+1;
flag=0;
end
if a(j,i)==0
flag=1;
end
end
if max<count
max=count;
end
end
% get the value of valuable max(length of matric c)
c=zeros(index1,max);
for i=1:1:index1
CL=1;
k=0;
for j=1:1:index2
if (a(i,j)==1)
k=k+1;
else
if k>0
c(i,CL)=k;
CL=CL+1;
k=0;
end
end
end
if k>0
c(i,CL)=k;
CL=CL+1;
k=0;
end
end
|