Convert matrix into the tensor

11 vues (au cours des 30 derniers jours)
Alan
Alan le 28 Jan 2012
Hi, I have some doubts about how can i convert a matrix into the Tensor... My situation: I have a matrix of 250X24, being that first column is associate the plant species while second column represent substances. However, for each substance were performed 22 experiments resulting on values. I want to convert this matrix into the tensor way where I have in the rows, the species, in the columns, substances and to each substance a third dimension corresponding the values of 22 experiments Here is a example of matrix,
1 60 0 1 1 0
2 60 0 1 1 0
3 11 1 1 1 0
3 17 1 1 1 0
3 18 1 1 1 0
3 20 1 1 1 0
3 22 1 1 1 0
3 28 1 1 1 0
3 33 1 1 1 0
3 37 1 1 1 0
3 48 1 1 1 0
3 49 1 1 1 0
3 52 1 1 1 0
3 55 1 1 1 0
4 11 3 1 1 0
4 17 3 1 1 0
first column are species, second, number of the substance, i.e., I want to organize this first row in the this manner: 1 row, sixth column and third dimension the rest (0 ,1 ,1, 0 son on)... Sorry If I don't get explain correctly Thanks
  1 commentaire
Andrew Newell
Andrew Newell le 29 Jan 2012
The number of elements (250x24) is not divisible by 22, so apparently not all 22 experiments were performed on each substance.

Connectez-vous pour commenter.

Réponse acceptée

Andrew Newell
Andrew Newell le 29 Jan 2012
This is not a tensor, which has a specific meaning. If you had the same number of experiments per substance, you could turn it into a 3D array. For a variable number of experiments you will need a cell array. Here is one approach:
First, determine the distinct species and substances. species = unique(M(:,1)); substances = unique(M(:,2));
Create a cell array of the appropriate size.
nspec = numel(species);
nsub = numel(substances);
Marray = cell(nspec,nsub);
Populate each combination of species and substance with a matrix of experimental results.
for ii=1:nspec
for jj=1:nsub
[r,c] = find(M(:,1)==species(ii) & M(:,2) == substances(jj));
Marray{ii,jj} = M(r,3:end);
end
end
Here is how you retrieve the results for species 3 and substance 22:
ii = find(species==3); jj = find(substances==22);
Marray{ii,jj}
ans =
1 1 1 0
  1 commentaire
Alan
Alan le 29 Jan 2012
Thanks,
It's working perfectly.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by