# [R] [EXT] Re: Initializing vector and matrices

Eik Vettorazzi E@Vettor@zz| @end|ng |rom uke@de
Thu Feb 29 10:42:36 CET 2024

```Dear Steven,
I used "sample" just to generate a non-trivial example, you could insert
your code of generating the real xi at this point :-)

If you want to stick to for-loops for some reasons, something like this
could work

x<-NULL
for (i in 1:5){
xi<-1:5
if (is.null(x)) x<-xi else x<-x+xi
}

cheers

Am 29.02.2024 um 09:23 schrieb Steven Yen:
> Hello Eik:
>
> Thanks. I do not need to sample. Essentially, I have a do loop which
> produces 24 vectors of length of some length (say k=300) and 24 matrices
> of 300x300. Then, I simply need to  take the averages of these 24
> vectors and matrices:
>
> x=(x1+x2+...+x24)/k
>
> y=(y1+y2+...+y24)/k
>
> I am just looking for ways to do this in a do loop, which requires
> initialization (to 0's) of x and y. My struggle is not knowning length
> of x until x1 is produced in the first of the loop. Thanks.
>
> Steven
>
> On 2/28/2024 6:22 PM, Eik Vettorazzi wrote:
>> Hi Steven,
>> It's not entirely clear what you actually want to achieve in the end.
>>
>> As soon as you "know" x1, and assuming that the different "xi" do not
>> differ in length in the real application, you know the length of the
>> target vector.
>> Instead of the loop, you can use 'Reduce' without having to initialize
>> a starting vector.
>>
>> # generate sample vectors, put them in a list
>>
>> xi<-lapply(1:5, \(x)sample(5))
>>
>> # look at xi
>> xi
>>
>> # sum over xi
>> Reduce("+",xi)
>>
>> this works also for matrices
>>
>> # generate sample matrices, put them in a list
>> Xi<-lapply(1:3, \(x)matrix(sample(16), nrow=4))
>>
>> # look at them
>> Xi
>>
>> # sum over Xi
>> Reduce("+",Xi)
>>
>> Hope that helps
>>
>> Eik
>>
>>
>> Am 28.02.2024 um 09:56 schrieb Steven Yen:
>>> Is there as way to initialize a vector (matrix) with an unknown
>>> length (dimension)? NULL does not seem to work. The lines below work
>>> with a vector of length 4 and a matrix of 4 x 4. What if I do not
>>> know initially the length/dimension of the vector/matrix?
>>>
>>> All I want is to add up (accumulate)  the vector and matrix as I go
>>> through the loop.
>>>
>>> Or, are there other ways to accumulate such vectors and matrices?
>>>
>>>  > x<-rep(0,4)  # this works but I like to leave the length open
>>>  >  for (i in 1:3){
>>> +  x1<-1:4
>>> +  x<-x+x1
>>> + }
>>>  > x
>>> [1]  3  6  9 12
>>>
>>>  > y = 0*matrix(1:16, nrow = 4, ncol = 4); # this works but I like to
>>> leave the dimension open
>>>       [,1] [,2] [,3] [,4]
>>> [1,]    0    0    0    0
>>> [2,]    0    0    0    0
>>> [3,]    0    0    0    0
>>> [4,]    0    0    0    0
>>>  > for (i in 1:3){
>>> +   y1<-matrix(17:32, nrow = 4, ncol = 4)
>>> +   y<-y+y1
>>> + }
>>>  > y
>>>       [,1] [,2] [,3] [,4]
>>> [1,]   51   63   75   87
>>> [2,]   54   66   78   90
>>> [3,]   57   69   81   93
>>> [4,]   60   72   84   96
>>>  >
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>

--
Eik Vettorazzi

Universitätsklinikum Hamburg-Eppendorf
Institut für Medizinische Biometrie und Epidemiologie

Christoph-Probst-Weg 1
4. Obergeschoss, Raum 04.1.021.1

20246 Hamburg

Telefon: +49 (0) 40 7410 - 58243
Fax:     +49 (0) 40 7410 - 57790

Web: www.uke.de/imbe

Webex: https://webteaching-uke.webex.com/meet/e.vettorazzi

--

_____________________________________________________________________

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Christian Gerloff (Vorsitzender), Joachim Prölß, Prof. Dr. Blanche Schwappach-Pignataro, Matthias Waldmann (komm.)
_____________________________________________________________________

SAVE PAPER - THINK BEFORE PRINTING
```