![]() |
数值替换
尊敬的朋友们:谢谢你们帮忙看看我这个程序(程序的意思是找出矩阵中的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; |
后半部分的程序
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) |
所有时间均为北京时间。现在的时间是 05:09。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.