Convert matrix into the tensor
조회 수: 11 (최근 30일)
이전 댓글 표시
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
Andrew Newell
2012년 1월 29일
The number of elements (250x24) is not divisible by 22, so apparently not all 22 experiments were performed on each substance.
채택된 답변
Andrew Newell
2012년 1월 29일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!