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!