查看单个帖子
旧 2009-03-27, 21:52   #4
laosam280
高级会员
 
注册日期: 2008-11-07
住址: 湖南长沙
帖子: 233
声望力: 21
laosam280 正向着好的方向发展
默认 回复: [求助]如何把一个包含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
laosam280 当前离线   回复时引用此帖