31 views (last 30 days)

Stephen Cobeldick
on 5 Aug 2016

Edited: Stephen Cobeldick
on 5 Aug 2016

In one line is easy and very fast:

>> M = [1,2,3;1,2,3;1,2,3];

>> sum(sum([M([1,end],2:end-1),M(1:end,[1,end]).']))

ans = 16

dpb
on 10 Feb 2018

[ ... Ricardo Florez Answer moved to comment since was a follow-up question on earlier Answer syntax ... dpb]

Hi. Why is it necessary to write two "sum" in the code: sum(sum(M(...)? Please see below:

the sum(M(:))-sum(sum(M(2:end-1,2:end-1)))

Thank you.

dpb
on 10 Feb 2018

Sign in to comment.

Sanjay Zamindar
on 28 Jun 2018

Edited: dpb
on 28 Jun 2018

function final_sum = peri_sum(A)

size_row = size(A,1)

size_col=size(A,2)

Sum_of_first_row = sum (A(1, [1:1:end])); %base

Sum_of_first_Colmn = sum(A([2:1:size_row],1))

%sum_col= sum(A([2:1:size_col], [1:size_col:size_col]));

Sum_of_last_column= sum(A([2:1:size_row],size_col))

sum_of_last_row = sum(A(size_row,[2:1:size_col-1]))

final_sum = sum_of_last_row+ Sum_of_last_column+Sum_of_first_Colmn+Sum_of_first_row

end

Sign in to comment.

Ibrahim Abouemira
on 19 May 2019

Hello,

Here's a function that performs your task.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.