How to access/manipulate data from cell arrays?

조회 수: 19 (최근 30일)
Thomas Veith
Thomas Veith 2019년 6월 3일
댓글: Adam Danz 2019년 6월 3일
So I have the code bleow which sovles an ODE 1000 times with random variables. The result is a 1000 by 1 cell array, the contents of which are all XX by 2 numeric arrays (column 1 solves x(1), column 2 solves x(2)). What I would like to produce is an XX by 1000 numeric array, where my 1000 columns would be comprised of all column 2 data from the numeric arrays nested in my cell array. Is this possible? I've looked through the Cell Arrays and Multilevel Indexing to Access Parts of Cells pages, and haven't been able to find what I'm looking for. Any help would be appreciated.
n = 1000;
result = cell(n,1);
for k=1:n
tspan=[1 7];A0=rand;P0=rand;g=rand;p=rand;B=rand;
[t,x] = ode45(@(t,x) [-g*x(1) + p*x(1); -x(1)*x(2)+ B*x(2)], tspan, [A0 P0]);
result{k} = x;
end

채택된 답변

Adam Danz
Adam Danz 2019년 6월 3일
편집: Adam Danz 2019년 6월 3일
This extracts column 2 data from all elements of the cell array and arranges it in a row within a cell array as you described.
C = cellfun(@(x)x(:,2),result,'UniformOutput', false)';
Here's how to put that into a padded array
% how much padding is needed per cell?
maxLen = max(cellfun(@(x)size(x,1),result)); %max length
padNum = maxLen - cellfun(@(x)size(x,1),result);
% get the col 2 vals and pad them
C = cellfun(@(x)x(:,2),result,'UniformOutput', false);
Cpad = cellfun(@(x,p)padarray(x,p,NaN,'post'),C,num2cell(padNum),'UniformOutput',false);
% Put them into a matrix
M = cell2mat(Cpad');
  댓글 수: 4
Thomas Veith
Thomas Veith 2019년 6월 3일
Thank you! What if they were all the same size? I get that I could write a matrix M123=[C{1,1},C{1,2},C{1,3},...,C{1,1000}]; but that would be quite tedious.
Adam Danz
Adam Danz 2019년 6월 3일
If they were all the same size, it would be as easy as
C = cellfun(@(x)x(:,2),result,'UniformOutput', false)';
cell2mat(C)

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 6월 3일
편집: madhan ravi 2019년 6월 3일
result{k} = x(:,2);
To produce a numeric matrix you need to append with nan or zeros at the end.
M=max(cellfun('prodofsize', result));
Desired=cell2mat(cellfun(@(x) [x;zeros(M-numel(x),1)], result,'un', 0))
  댓글 수: 2
Thomas Veith
Thomas Veith 2019년 6월 3일
Thank you for your quick reply! That works very well, however, it's not exactly what I was looking for. That changes my result so that my cell array only includes the results from column 2, each selection of which represents a numerica array of varying size (41x1, 285x1, etc, etc). That is nice, and more managable than including both x solutions, but, what I was really hoping for was that I could produce a YYx1000 numeric array, so that column one would be the 41 entries, column two the 285 entries, etc, etc. Is this possible? Thanks in advance.
madhan ravi
madhan ravi 2019년 6월 3일
See edited answer.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by