Functions on subsets of vector
Mostrar comentarios más antiguos
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 comentarios
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?
Cedric
el 10 de Mzo. de 2013
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?
Image Analyst
el 10 de Mzo. de 2013
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?
Respuestas (2)
wsq= cumsum(w.^2);
idx=find(diff(t) | diff(g));
ww=[wsq(idx), wsq(end)] - [0,wsq(idx)],
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 comentarios
Daan
el 10 de Mzo. de 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.
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.
Matt J
el 10 de Mzo. de 2013
Aww, it's just a small tweak. And we don't yet know if it's what Daan needs.
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!