Matrix Sub Matrix Error

2 vues (au cours des 30 derniers jours)
Amit
Amit le 15 Fév 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.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 15 Fév 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');
  1 commentaire
Amit
Amit le 15 Fév 2012
Thanks andrei

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by