Info

此问题已关闭。 请重新打开它进行编辑或回答。

Element by element operation

1 次查看(过去 30 天)
John Draper
John Draper 2016-6-25
关闭: Stephen23 2016-6-25
Hi everyone,
I have some 3x3 matrix e.g. a:
if true
a = [1 2 3; 4 5 6; 7 8 9];
I would like to take an individual element and then subtract the surrounding elements to create a 3x3 sub array in the larger 9x9 array. I have written some code that will do this for me:
if true
Av1 = a(1,1) - a(1:3,1:3);
Av2 = a(1,2) - a(1:3,1:3);
Av3 = a(1,3) - a(1:3,1:3);
Av4 = a(2,1) - a(1:3,1:3);
Av5 = a(2,2) - a(1:3,1:3);
Av6 = a(2,3) - a(1:3,1:3);
Av7 = a(3,1) - a(1:3,1:3);
Av8 = a(3,2) - a(1:3,1:3);
Av9 = a(3,3) - a(1:3,1:3);
Av = [Av1 Av2 Av3; Av4 Av5 Av6; Av7 Av8 Av9];
However as you can see this code is pretty inelegant and cannot be applied to arrays of different sizes. My question is whether it is possible to shorten the code and make it applicable to arrays of different sizes. Thanks for your time, John.

回答(0 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by