How to extract data from Cell?

조회 수: 4 (최근 30일)
srini
srini 2019년 4월 21일
댓글: srini 2019년 4월 21일
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.

채택된 답변

Walter Roberson
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)
  댓글 수: 1
srini
srini 2019년 4월 21일
Great, Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by