"Pablo " <munguia@mail.utexas.edu> wrote in message <ic1gbf$g20$1@fred.mathworks.com>...
>
> With a matrix of
> T S V
> 1 1 0.78
> 1 2 0.93
> 2 1 0.14
> 2 1 0.57
> 2 2 0.98
> 3 1 0.47
>
> Where the first column (T) is a grouping variable (with three levels), and the second column (S) is a grouping variable with two levels embedded within the first. The third column (V) are data.
>
> I want to calculate pdist (the actual data has more than one row for each grouping variable) for each group of S data within T.
>
> Using:
> sortrows(data,[1:4]);
> newdata = newdata(:,5:end);
>
> certainly helps to sort the data, but the problem is still how to calculate data for subsets of the matrix of different lengths? It seems like a very easy thing to do, but I am too dense at this point.
>
> Thanks again!
> Pablo
           
The one inconvenience you will face is that for the various groups you apply 'pdist' to, there will be a varying length result which you will need to store, presumably in a cell array. Matlab's regular arrays are not constituted to handle variable length collections of data.
Assume that your original matrix, M, has 'a' rows and 'b+c' columns in which the first 'b' columns are your "dummy" vectors and the 'c' columns are your data columns to which pdist is to be applied. Then do this:
[~,m,n] = unique(M(:,1:a),'rows','last'); %Get all unique combinations of "dummies"
[~,p] = sort(n);
q = (1:length(p)).';
q(p) = q;
q = [0;q(m)];
At this point you are ready to apply p and q to extracting groups from M which all have the same entries in M's columns 1 to a. You would presumably have a forloop something like this:
for k = 1:length(m) % One loop for each group
1. Then pass the array M(p(q(k)+1:q(k+1)),a+1:b) to pdist
2. Put the variable length result in the kth cell of a cell array
3, Do whatever else you require with each group
end
The expression "p(q(k)+1:q(k+1))" gives you all the row indices in M which belong to the kth grouping. That is, within each group the sets of column elements 1:a are all the same.
Roger Stafford
