Matrix Sub Matrix Error

2 次查看(过去 30 天)
Amit
Amit 2012-2-15
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.

采纳的回答

Andrei Bobrov
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 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by