Heidi Wong
2008-Nov-28 07:35 UTC
[R] question involving loops from intro level R programming class
a. Write a R function zerdiag.v1(m) using loop to output a square matrix whose diagonal elements are zero and the other elements are filled in by consecutive integers from 1 to m row-wise. For example, zerdiag.v1(6) = [0, 1, 2] [3, 0, 4] [5, 6, 0] This function should have error checking ability. If the input m cannot form a square matrix, then the function will return an error message: "Input number is incorrect." b. Write a R function zerdiag.v2(m) to produce the same output as in part (a) without using a loop. c. Test your functions in part (a) and (b) using m=12 and m=14 respectively. I'd appreciate any help with this problem... I've spent a lot of time staring at it, and I'm still not sure where to start. Thanks! [[alternative HTML version deleted]]
Richard.Cotton at hsl.gov.uk
2008-Nov-28 10:02 UTC
[R] question involving loops from intro level R programming class
> a. Write a R function zerdiag.v1(m) using loop to output a square matrix > whose diagonal elements are zero and the other elements are filled in by > consecutive integers from 1 to m row-wise. > > For example, > zerdiag.v1(6) = [0, 1, 2] > [3, 0, 4] > [5, 6, 0] > > This function should have error checking ability. If the input m cannotform> a square matrix, then the function will return an error message: "Input > number is incorrect." > > b. Write a R function zerdiag.v2(m) to produce the same output as inpart> (a) without using a loop. > > c. Test your functions in part (a) and (b) using m=12 and m=14respectively.> > I'd appreciate any help with this problem... I've spent a lot of time > staring at it, and I'm still not sure where to start. Thanks!As the posting guide (http://www.r-project.org/posting-guide.html) says "Basic statistics and classroom homework: R-help is not intended for these. " Also, I find it a little bit cheeky that you''ve asked for help on 3 parts of your homework, and haven't provided any evidence that you've even attempted the problem yourself. (If you've been staring at the problem for hours, you must have written something.) Read the help pages ?matrix and ?"for" and take a look in the Intro to R manual. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Gustaf Rydevik
2008-Nov-28 11:14 UTC
[R] question involving loops from intro level R programming class
On Fri, Nov 28, 2008 at 8:35 AM, Heidi Wong <hungryhungryheidi at gmail.com> wrote:> a. Write a R function zerdiag.v1(m) using loop to output a square matrix > whose diagonal elements are zero and the other elements are filled in by > consecutive integers from 1 to m row-wise. > > For example, > zerdiag.v1(6) = [0, 1, 2] > [3, 0, 4] > [5, 6, 0] > > This function should have error checking ability. If the input m cannot form > a square matrix, then the function will return an error message: "Input > number is incorrect." > > b. Write a R function zerdiag.v2(m) to produce the same output as in part > (a) without using a loop. > > c. Test your functions in part (a) and (b) using m=12 and m=14 respectively. > > I'd appreciate any help with this problem... I've spent a lot of time > staring at it, and I'm still not sure where to start. Thanks! > > [[alternative HTML version deleted]] >Hi, Nice little brain teaser! Not too difficult, but requires a bit of creative thinking... You might wanna have a look at, for example, ?diag, ?uniroot, or ?polyroot. regards, Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik