MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Thread Subject: An even nastier vector operation!

 Subject: An even nastier vector operation! From: Ulrik Nash Date: 18 Mar, 2010 16:55:21 Message: 1 of 7 Hi Everyone, In extension to a problem posted yesterday (Subject: Complicated vector operation!), I am having trouble with a perhaps even nastier version! Suppose I have a horizontal vector, "A". For example: A = [2 3 5 5 1 4] Suppose also that I have a vertical vector of integers: number_in_sample = [1 3 5]' Now I wish to create a vertical vector, called "averages", which consists of averages of numbers drawn from A, where the number of elements drawn (without replacement), is defined by the vector number_in_sample. So, the number of elements in "averages", is equal to the number of elements in "number_in_sample". Can this be done without the need to loop? I appreciate the help. Regards, Ulrik.
 Subject: An even nastier vector operation! From: us Date: 18 Mar, 2010 17:16:03 Message: 2 of 7 "Ulrik Nash" wrote in message ... > Hi Everyone, > > In extension to a problem posted yesterday (Subject: Complicated vector operation!), I am having trouble with a perhaps even nastier version! > > Suppose I have a horizontal vector, "A". For example: > > A = [2 3 5 5 1 4] > > Suppose also that I have a vertical vector of integers: > > number_in_sample = [1 3 5]' > > Now I wish to create a vertical vector, called "averages", which consists of averages of numbers drawn from A, where the number of elements drawn (without replacement), is defined by the vector number_in_sample. > > So, the number of elements in "averages", is equal to the number of elements in "number_in_sample". > > Can this be done without the need to loop? > > I appreciate the help. > > Regards, > > Ulrik. of course, no problem... us
 Subject: An even nastier vector operation! From: ImageAnalyst Date: 18 Mar, 2010 17:20:51 Message: 3 of 7 So you have the number of elements to average together, but what are their indices? Let's say you are looking at number_in_sample(2) - it says to average 3 elements of A. But which three?!?
 Subject: An even nastier vector operation! From: us Date: 18 Mar, 2010 17:37:02 Message: 4 of 7 ImageAnalyst wrote in message ... > So you have the number of elements to average together, but what are > their indices? Let's say you are looking at number_in_sample(2) - it > says to average 3 elements of A. But which three?!? assumption: random draws... us
 Subject: An even nastier vector operation! From: Matt Fig Date: 18 Mar, 2010 17:51:04 Message: 5 of 7 If I understand correctly: C = cumsum(A); C = C(B)./(B.')
 Subject: An even nastier vector operation! From: Ulrik Nash Date: 18 Mar, 2010 18:34:21 Message: 6 of 7 So to clarify, in the example I have given, the first number in the "averages" vector would consist of: Element 1) The sum of 1 randomly drawn number from A divided by 1 Element 2) The sum of 3 randomly drawn number from A divided by 3 Element 3) The sum of 5 randomly drawn number from A divided by 5 Note that each of the random numbers drawn to form a given element are drawn without repleacement, BUT there IS replacement once a given element has been computed and it is time for the next element is calculated. I do hope this helps! :-) Regards, Ulrik.
 Subject: An even nastier vector operation! From: Matt Fig Date: 18 Mar, 2010 19:22:19 Message: 7 of 7 O.k, try again. I am not convinced this will be faster than a loop approach. % Data A = [2 3 5 5 1 4] B = [1 3 5]' % Engine L = length(B); [S,S] = sort(rand(L,length(A)),2); C = cumsum(A(S),2); C = C((1:L)+(B.'-1)*L)./(B.')