Convert matrix into the tensor
11 views (last 30 days)
Show older comments
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 Comment
Andrew Newell
on 29 Jan 2012
The number of elements (250x24) is not divisible by 22, so apparently not all 22 experiments were performed on each substance.
Accepted Answer
Andrew Newell
on 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
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!