sumRings - If % the input array is empty, the ouput should...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 %========================================================================== % Problem 3. Arrays and Recursion %-------------------------------------------------------------------------- % % Problem Statment: % % Write a recursive function named sumRings that 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

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
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). % % Notes: % - You may *only* use the following built-in functions: size, sum, % isempty, or length. % - You *must* use recursion to solve this problem. % function res = sumRings(X) [r,c] = size(X) res= if r==1 || c==1 res=[res sum(X)] elseif r==2 || c==2 res=[res sum(sum(X))] elseif r==0 && c==0 res=[res ] elseif c>2 a=sum(X) A=a(1)+a(c) X(:,c)= X(:,1)= [r1 c1]=size(X') if r1==1 b=X' B=b(1)+b(r) X(r,:)= X(1,:)= [r,c] = size(X) Sheet1 Page 2 else b=sum(X') B=b(1)+b(r) X(r,:)= X(1,:)= [r,c] = size(X) end res=[res (A+B) sumRings(X)] end...
View Full Document

This note was uploaded on 07/06/2009 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 2

sumRings - If % the input array is empty, the ouput should...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online