How to extract data from Cell?
23 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am looking to extract data from a cell comprising matrices of different shapes. Let's say I have a cell structure a containing matrix of different dimensions in its cell.
a{1,1}=[1,10,30];
a{2,1}=[6,15,20];
a{3,1}=[10,15,35];
a{4,1}=[100,150,200];
I would like to extract the data based on cell row index and at the same time based on the column index within the cell. Something like extracting the first, second, third column in the cell [1,6,10] , [10,15,15] and [30,20,35] from a{1:3,1}.
Though the following code able to extract the elements column wise from the structure, I couldn't make able to specify the row index of the cell.
FirstColumn=[cellfun(@(x) x(1,1),a,'UniformOutput',true)]'
SecondColumn=[cellfun(@(x) x(1,2),a,'UniformOutput',true)]'
ThirdColumn=[cellfun(@(x) x(1,3),a,'UniformOutput',true)]'
When I tried using indexing using {}, I able to find individual elements in a cell but not a vector
a{1,1}(1,1)
%Works! Out=1\
a{1,1}(1,2)
%Out=10
a{1,1}(1,3)
%Out=30
a{1:3,1}(1,1) % Result in Error "Expected one output from a curly brace or dot indexing expression, but there were 3 results"
Could anyone please suggest me a way to extract the elements ? Thanks.
0 件のコメント
採用された回答
Walter Roberson
2019 年 4 月 21 日
cellfun(@(M) M(1,ColumnToSelect), a(RowsToSelect))
Or in the special case of the cell containing row vector of the same length
T = vertcat(a{RowsToSelect}) ;
T(:, ColumnsToSelect)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!