Info

This question is closed. Reopen it to edit or answer.

Element by element operation

1 view (last 30 days)
John Draper
John Draper on 25 Jun 2016
Closed: Stephen23 on 25 Jun 2016
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.

Answers (0)

This question is closed.

Community Treasure Hunt

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

Start Hunting!