フィルターのクリア

Convert matrix into the tensor

4 ビュー (過去 30 日間)
Alan
Alan 2012 年 1 月 28 日
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
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
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
  1 件のコメント
Alan
Alan 2012 年 1 月 29 日
Thanks,
It's working perfectly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAutomotive Applications についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by