How to extract data from Cell?
19 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Walter Roberson
on 21 Apr 2019
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)
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!