Matrix Sub Matrix Error
2 次查看(过去 30 天)
显示 更早的评论
I want to place element of matrix g as g(11)=(a11+a12+a21+a22)/4 and for last column and last row I want to add g(n1)=(an1+a11+an2+a12)/4
m=[1:5;2:6;3:7;4:8;5:9;];
width=length(m(1,:)); g=zeros(width);
%% for a=1:width for b=1:width e=a+1; f=b+1; if ( e > width ) e=1; if ( f > width ) f=1; end end
g(a,b) = (m(a,b)+m(e,b)+m(a, f)+m(e,f))/4;
end
end
g
Showing error as ??? Attempted to access local_trandata(1,6); index out of bounds because size(local_trandata)=[5,5].
Please tell me how i can resolve this error.
0 个评论
采纳的回答
Andrei Bobrov
2012-2-15
width=length(m(1,:));
g=zeros(width);
for a=1:width
for b=1:width
e=a+1;
f=b+1;
if ( e > width )
e=1;
end
if ( f > width )
f=1;
end
g(a,b) = (m(a,b)+m(e,b)+m(a, f)+m(e,f))/4;
end
end
g
OR
m1 = zeros(size(m)+1);
m1(1:end-1,1:end-1)=m;
m1(end,:) = m1(1,:);
m1(:,end) = m1(:,1);
g = conv2(m1,[1 1;1 1]*.25,'valid');
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!