Functions on subsets of vector

Dear forum users, I am quite new to Matlab, took some intro-courses past 2 days and now I have the following issue: I have three vectors and from one of them (vector w) I want to do the following multiplication: ww'. However, I want Matlab to do this only for subsets of w, which are based upon the other two vectors. Vector t contains the time of each observation and g contains an indication of a group the observation in w belongs to. w contains some weights.
For example:
t [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2]'
g [6 6 6 6 7 7 7 9 9 9 9 9 9 9 9 4 4 4 4 4]'
w [0.1 0.3 0.45 0.56 0.56 etc etc ]'
I want Matlab to do the the ww' first for the first 4 observations because they are in t=1 and g=6, then I want Matlab to do this for observation 5 until 4 because those observations belong to t=1 and g=7 and so forth. What may be important to note is that each subset can contain a different number of observations.
How can I approach this problem? I actually want to do this for a rather large vectors so it should be a method that is efficient as well. The output should off course show the outcome of each ww'.

4 Comments

Cedric
Cedric on 10 Mar 2013
Edited: Cedric on 10 Mar 2013
What happens if the time changes within a group? If it doesn't, then your block-indexing is only based on g. Also, wi * wi.' will be a square matrix for wi a subset of w, as the latter is a column vector. Is that what you want or do you want a scalar product?
Daan
Daan on 10 Mar 2013
Edited: Daan on 10 Mar 2013
Good that you mention this: i actually want a scalar product so it should be w'w.
About the t & g:
Most of the groups are actually represented in each time period t: e.g.:
t [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 etc]'
g [6 6 6 6 7 7 7 9 9 9 9 9 9 9 9 4 4 4 4 4 6 6 6 etc]'
Originally, it was data in SAS. I first sorted on time t and then on group g.
Would the fact that group g is represented in almost all time period t complicate the matter? Perhaps it is important to stress "almost".
but how should be treated a situation in which we would have
ti = [1 1 1 2 2]
wi = [7 8 5 4 9]
for group 9 (meaning that elements of g are 9 in block i). Can this situation happen, and if so, how would you treat it? Should group 9 be split in two subgroups?
Assuming you meant g instead of wi, as I understand it, there would be 5 groups there because he defines as group as a run of elements where BOTH t is constant AND g is constant. Anytime either t or g changes value, then a new group begins. Am I correct?

Sign in to comment.

Answers (2)

Matt J
Matt J on 10 Mar 2013
Edited: Matt J on 10 Mar 2013
wsq= cumsum(w.^2);
idx=find(diff(t) | diff(g));
ww=[wsq(idx), wsq(end)] - [0,wsq(idx)],
Cedric
Cedric on 10 Mar 2013
Edited: Cedric on 10 Mar 2013
If the answer to my comment above is that t doesn't matter, an answer to your question could be:
sc = accumarray(g, w.^2) ;
where sc(i) is the scalar product of the block in w that correspond to group i.
I illustrate it on a simple case where elements of w are 1's, so scalar products should just be the size of each group (which we can easily see/check), we get:
>> g = [6 6 6 1 1 2 2 2 2 4].' ;
>> w = ones(size(g)) ;
>> sc = accumarray(g, w.^2)
sc =
2
4
0
1
0
3
This solution doesn't work if t matters though, e.g. if different values of t should lead to splitting groups into sub-groups.

5 Comments

Really appreciate the comments! Unfortunately, I think the t does matter. However, your comment still has value because it introduced me to the accumarray function.
I am now thinking of creating some kind of workaround solution by combining the t and g into 1 category for each t and g combination. If that works out I can still use your method.
Cedric
Cedric on 10 Mar 2013
Edited: Cedric on 10 Mar 2013
So when t varies within a group, the group must be split into sub-groups? If so, it means that you don't really need/want to keep the information about group IDs, but you just want to split w into chunks each time the time or the group varies. In that case, Matt gave you a working solution. He spots locations where there is a change in either t or g and the uses the locations to break out the cumulative sums at relevant places for differences to equal scalar products of chuncks of w.
Matt J
Matt J on 10 Mar 2013
Edited: Matt J on 11 Mar 2013
Daan: I am now thinking of creating some kind of workaround solution by combining the t and g into 1 category for each t and g combination.
No, that's not necessary. The simple modification would be to let sc be 2-dimensional, and run accumarray with just the minor change
sc = accumarray([t(:),g(:)], w.^2);
Now sc(T,G) corresponds to subset t=T and g=G.
Cedric: He spots locations where there is a change in either t or g and the uses the locations to break out the cumulative sums at relevant places for differences to equal scalar products of chuncks of w.
If members of a given subset can occur non-sequentially in w (when a given pair t,g recurs non-sequentially) then the cumsum approach won't work.
Cedric
Cedric on 10 Mar 2013
Edited: Cedric on 10 Mar 2013
Matt, you have my vote if you change your comment into an answer. I wouldn't have thought about your change!
Aww, it's just a small tweak. And we don't yet know if it's what Daan needs.

Sign in to comment.

Categories

Asked:

on 10 Mar 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!