Got Questions? Get Answers.
Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matrix reshaping problems

Subject: matrix reshaping problems

From: Pablo

Date: 17 Nov, 2010 03:34:06

Message: 1 of 5

Hello,
I am trying to reshape a matrix where the first four vectors are dummy (nominal) variables:

[1 1 1 1 data1 data2 data3 ... data9 ]
..........
[4 3 2 3 data26 ... ]
These first four nominal variables correspond to 4 Sites, 3 Treatments, 2 Sexes, and 3 Times. The matrix has dimensions of 1876 x 9 cells because the data distribution is not completely balanced. Because of this, a simple reshaping of the data matrix into a single vector is not possible.

For each set of site, treatment, sex, time group (between 15 and 30 rows of data) - there is a series of statistical calculations.

I created a series of loops to allow for these calculations, one set at a time using
for example:

for i=1:3 %identifying sites
idi=(data(:,1)==i); %identifies data for a particular site
site=data(idi,2:end); %reduces the data matrix to just those belonging to site=i.

.... And basically reducing the data matrix with a loop for each dummy variable until the matrix has the size of only an appropriate time (again, between 15 and 30 rows of code).
  
However, this seems too primitive and completely inelegant method. and mdscale function tends to choke due to the lack of balance at times.
Any help would be appreciated.
Thanks!
Pablo

Subject: matrix reshaping problems

From: Roger Stafford

Date: 17 Nov, 2010 04:19:04

Message: 2 of 5

"Pablo " <munguia@mail.utexas.edu> wrote in message <ibvife$k05$1@fred.mathworks.com>...
> Hello,
> I am trying to reshape a matrix where the first four vectors are dummy (nominal) variables:
>
> [1 1 1 1 data1 data2 data3 ... data9 ]
> ..........
> [4 3 2 3 data26 ... ]
> These first four nominal variables correspond to 4 Sites, 3 Treatments, 2 Sexes, and 3 Times. The matrix has dimensions of 1876 x 9 cells because the data distribution is not completely balanced. Because of this, a simple reshaping of the data matrix into a single vector is not possible.
>
> For each set of site, treatment, sex, time group (between 15 and 30 rows of data) - there is a series of statistical calculations.
>
> I created a series of loops to allow for these calculations, one set at a time using
> for example:
>
> for i=1:3 %identifying sites
> idi=(data(:,1)==i); %identifies data for a particular site
> site=data(idi,2:end); %reduces the data matrix to just those belonging to site=i.
>
> .... And basically reducing the data matrix with a loop for each dummy variable until the matrix has the size of only an appropriate time (again, between 15 and 30 rows of code).
>
> However, this seems too primitive and completely inelegant method. and mdscale function tends to choke due to the lack of balance at times.
> Any help would be appreciated.
> Thanks!
> Pablo
- - - - - - - -
  Your description is not very clear. I am guessing you need the 'sortrows' function.

 newdata = sortrows(data,[1:4]);
 newdata = newdata(:,5:end);

  However this is a rather wild guess. I think you should have another try at explaining your problem.

Roger Stafford

Subject: matrix reshaping problems

From: Pablo

Date: 17 Nov, 2010 21:10:07

Message: 3 of 5

  Your description is not very clear. I am guessing you need the 'sortrows' function.

 newdata = sortrows(data,[1:4]);
 newdata = newdata(:,5:end);

  However this is a rather wild guess. I think you should have another try at explaining your problem.

Roger Stafford
__________________________________________

Thanks for the reply, hopefully this is a better explanation.

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

Subject: matrix reshaping problems

From: Roger Stafford

Date: 18 Nov, 2010 01:15:06

Message: 4 of 5

"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 for-loop 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 k-th 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 k-th grouping. That is, within each group the sets of column elements 1:a are all the same.

Roger Stafford

Subject: matrix reshaping problems

From: Pablo

Date: 19 Nov, 2010 16:25:06

Message: 5 of 5


> 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 for-loop 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 k-th 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 k-th grouping. That is, within each group the sets of column elements 1:a are all the same.
>
> Roger Stafford

_______________________________
Hello again,
Thanks, this certainly helps!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us