MATLAB: How to find the sum of the perimeter of a matrix

matrixperimetersum

Hi, I am wondering if I had a matrix [1,2,3;1,2,3;1,2,3] how I could find the sum of the perimeter. Thanks an advance.

Best Answer

One way amongst any number...
>> M=[1,2,3;1,2,3;1,2,3];
>> I=ones(size(M)); I(2:end-1,2:end-1)=0;
>> S=sum(reshape(M.*I,1,[]))
S =
16
>>