[R] Question involving loops

Tom Backer Johnsen backer at psych.uib.no
Fri Nov 28 12:20:51 CET 2008


hwong1 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.
> 
> Any help with this question would be much appreciated. Thank you!

This looks like an assignment to me.

Tom


-- 
+----------------------------------------------------------------+
| Tom Backer Johnsen, Psychometrics Unit,  Faculty of Psychology |
| University of Bergen, Christies gt. 12, N-5015 Bergen,  NORWAY |
| Tel : +47-5558-9185                        Fax : +47-5558-9879 |
| Email : backer at psych.uib.no    URL : http://www.galton.uib.no/ |
+----------------------------------------------------------------+



More information about the R-help mailing list