Matrix Sub Matrix Error

2 views (last 30 days)
Amit
Amit on 15 Feb 2012
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.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 15 Feb 2012
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');

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!