Write a function called spiral_diag_sum that takes an odd positive integer n as an input and computes the sum of all the elements in the two diagonals of the n-by-n spiral matrix.

3 次查看(过去 30 天)
function [ MySum ] = spiral_diag_sum( n )
MySum=1;
if n==1
return
end
for i=3:2:n
mult=0;
j=0;
while j <= (i-3)/2
mult=mult+j;
j=j+1;
end
MySum=MySum+(4*(i+mult*8)+6*(i-1));
end
end
this for some reason works but i dont undestand how. What does the while loop do? And how do you figure out the MySum part?
  2 个评论
Jorge Briceño
Jorge Briceño 2018-2-4
Hi everyone,
I came up with a different solution.
function [ sumS ] = spiral_diag_sum( n )
% If statement for n = 1.
sumS = 1;
if n == 1
return
% If statement for n > 1
elseif n>1
% Value when n = 1.
for counter = n:-2:2
% This code sums the corner values of each layer from a 3 x 3 to a n x n
% matrix. The counter is evaluating the layers values.
sumS = sumS + sum (counter^2:-(counter-1):(counter-2)^2+1);
end
end
end
Champions2015, maybe you should try understanding the problem first. Use simple code such as:
(counter^2:-(counter-1):(counter-2)^2+1)
With a counter = 3 and 5, pay attention to the values. Your code is using a formula a bit complex to understand.
I hope it helps.
William Gallego
William Gallego 2018-11-4
I always like your solutions and explanations. Thanks again!
function s = spiral_diag_sum(n)
if n==1
s=1;
else
a=spiral(n);
b=sum(diag(a));
a(1:end,:)=a(end:-1:1,:);
c=sum(diag(a));
d=a((1+n)/2,(1+n)/2);
s=b+c-d;
end

请先登录,再进行评论。

采纳的回答

Anh Tran
Anh Tran 2017-9-6
There are many ways to solve this coding problem. The most common way is to divide the spiral matrix into layers, find a pattern and accumulate the sum of 4 corners of each layer.
You may refer to the following link, but keep in mind that this code uses a different pattern for sum of each layer's corner. Hint: start from bottom right instead of top right http://www.geeksforgeeks.org/sum-diagonals-spiral-odd-order-square-matrix/
  1 个评论
Anupriya Krishnamoorthy
编辑:Anupriya Krishnamoorthy 2018-2-19
Thank you for the link, I learnt and I have done it in a different way with just 2 lines of codes.
function out = spiral_diag_sum(n)
A = 3:2:n
out = 1 + sum( 4*A.^2 - 6*(A-1)) % The formula "f(n) = 4n^2-6(n-1)+f(n-2)"
end

请先登录,再进行评论。

更多回答(3 个)

Erfan Pakdamanian
Erfan Pakdamanian 2018-5-23
编辑:Erfan Pakdamanian 2018-5-23
function s= spiral_diag_sum(n)
sp=spiral(n);
s=sum(diag(sp))+sum(diag(fliplr(sp)))-1;

Vignesh M
Vignesh M 2018-5-8
%if you dont want recursion & want to use loops instead, make use of below code
function sum = spiral_diag_sum(n)
if n == 1
sum = 1;
else
sum_it = 0;
m = 1:2:n;
for m = m(m~=1)
sum_it = 4*(m^2) - 6*(m-1) + sum_it ;
end
sum = sum_it + 1 ; % added 1(center element)
end
end

Govind Sankar Madhavan Pillai Ramachandran Nair
Hi, I am trying to attempt the same question. But my problem is where is the spiral MATRIX.How do we compute the sum without the MATRIX.
function spiralsum = spiral_diag_sum(n) spiralsum = 0; for i = 1:n for j = 1:n if(i==j) spiralsum = spiralsum + M(i,j); end end end spiralsum = spiralsum + sum(M(n*n:-(n-1):1)) - M(1,1) - M(n,n)-M((floor(n/2))+1,(floor(n/2))+1) end

类别

Help CenterFile Exchange 中查找有关 Encryption / Cryptography 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by