ringSum HW9

ringSum HW9 - If % the input array is empty, the ouput...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
% 10/14/2009 4:12 PM % Function Name: ringSum % Inputs: 1.(Double) Vector of numbers. % Outputs: 1.(Double) Vector of sums of rings, innermost first % % Function Description: % Write a function named ringSum that recursively consumes an M by N % array and returns a vector of the sums of its rings of elements. % Consider: % [1 1 1 1 1 1 1 % 1 2 2 2 2 2 1 % 1 2 3 3 3 2 1 % 1 2 2 2 2 2 1 % 1 1 1 1 1 1 1] % % [1 1 1 1 1 1 1 % 1 2 2 2 2 2 1 % 1 2 2 2 2 2 1 % 1 1 1 1 1 1 1] % In the first instance, there are three rings of identical elements. In % the second example there are only two rings. If the input array is a % 1xN or Mx1 (row or column vector), find the sum of its elements.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: If % the input array is empty, the ouput should be an empty vector (*not* % zero). The function should return the sums in order of most inward % ring first and moving outward from there. % % Notes: % - You may *only* use the following built-in functions: size, sum, % isempty, or length. % - You must use recursion to receive any credit. % function out = ringSum(in) [r c] = size(in); if isempty(in) out = ; elseif r<=2 || c<=2 out = sum(sum(in)); else out = [ringSum(in(2:end-1, 2:end-1)) sum(sum(in))-sum(ringSum(in(2:end-1, 2:end-1)))]; end...
View Full Document

Ask a homework question - tutors are online