Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
初级会员
注册日期: 2010-07-25
年龄: 41
帖子: 4
声望力: 0 ![]() |
![]()
尊敬的朋友们:谢谢你们帮忙看看我这个程序(程序的意思是找出矩阵中的0值,在用周围的数据平均代替这个0值),但是我编程遍历了所有的0值,但是在替换过程中,我的非0值也被替换了,谢谢各位帮我看看问题所在!!谢谢
A=[0 0 0; 0 2 0;0 2 0 ;0 0 0]; [row,col]=find(A==0); for i=1:length(row) for j=1:length(col) if row(i)==1 & col(j)==1 %第一行第一个元素 A(row(i),col(j))=(A(row(i),col(j)+1)+A(row(i)+1,col(j)+1)+A(row(i)+1,col(j)))/3; else if row(i)==1 & col(j)==max(length(A(1, ![]() A(row(i),col(j)) = (A(row(i),col(j)-1)+A(row(i)+1,col(j)-1)+A(row(i)+1,col(j)))/3; else if row(i)==max(length(A(:,1))) & col(j)==1%最后一行第一个数 A(row(i),col(j)) = (A(row(i)-1,col(j))+A(row(i)-1,col(j)+1)+A(row(i),col(j)+1))/3; else if row(i)==max(length(A(:,1))) & col(j)==max(length(A(1, ![]() A(row(i),col(j)) = (A(row(i)-1,col(j))+A(row(i)-1,col(j)-1)+A(row(i),col(j)-1))/3; else if row(i)>=2 & row(i)< max(length(A(:,1))) & col(j)==1%第一列的中间数 A(row(i),col(j)) = (A(row(i)-1,col(j))+A(row(i)-1,col(j)+1)+A(row(i),col(j)+1)+A(row(i)+1,col(j)+1)+A(row(i)+1,col(j)))/5; else if row(i)>=2 & row(i)< max(length(A(:,1))) & col(j)==max(length(A(1, ![]() A(row(i),col(j)) = (A(row(i)+1,col(j))+A(row(i)+1,col(j)-1)+A(row(i),col(j)-1)+A(row(i)-1,col(j)-1)+A(row(i)-1,col(j)))/5; else if row(i)==1&col(j)>=2 & col(j)<max(length(A(1, ![]() A(row(i),col(j)) = (A(row(i),col(j)-1)+A(row(i)+1,col(j)-1)+A(row(i)+1,col(j))+A(row(i)+1,col(j)+1)+A(row(i),col(j)+1))/5; |
![]() |
![]() |
![]() |
#2 |
初级会员
注册日期: 2010-07-25
年龄: 41
帖子: 4
声望力: 0 ![]() |
![]()
else if row(i) == length(A(:,1)) & col(j) >=2 & col(j)<length(A(1,
![]() A(row(i),col(j)) =(A(row(i),col(j)-1)+A(row(i)-1,col(j)-1)+A(row(i)-1,col(j))+A(row(i)-1,col(j)+1)+A(row(i),col(j)+1))/5; else row(i) >= 2 & row(i) < max(length(A(:,1))) & col(j) >=2 & col(j)<max(length(A(1, ![]() A(row(i),col(j))=(A(row(i),col(j)-1)+A(row(i)-1,col(j)-1)+A(row(i)-1,col(j))+A(row(i)-1,col(j)+1)+A(row(i),col(j)+1)+A(row(i)+1,col(j)+1)+A(row(i)+1,col(j))+A(row(i)+1,col(j)-1))/8; end end end end end end end end end end disp(A) |
![]() |
![]() |