build a matrix from cell

Dear
I have a cell array which I need to select only first column of each cell separately and put it into a matrix. But each column has different number of row.
can somebody help me ? I think I can build a matrix of zero with the size of my cell and then I put each column separately. However, following effort does not work.
[n,m]=size(X)
A = zeros(100,n);
for i=1:n
A (:,n) = X{n}(:,1)
end

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 8일

0 개 추천

X={magic(4),eye(4),rand(4)} % Example
y=cell2mat(cellfun(@(x) x(:,1),X,'un',0))

댓글 수: 3

Niki
Niki 2013년 5월 8일
편집: Niki 2013년 5월 8일
Thanks but That is not working for when we have your example X but transposed
X={magic(4),eye(4),rand(4)} % example
X=X';
Now I want to get the first column of each cell into a matrix
y=cell2mat(cellfun(@(x) x(:,1)',X,'un',0))'
Niki
Niki 2013년 5월 8일
편집: Niki 2013년 5월 8일
Again thanks but as I explained from very beginning the row of each column is not equal, by running that one I get this error
??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 85
m{n} = cat(1,c{:,n});

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

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 8일
편집: Azzi Abdelmalek 2013년 5월 8일

0 개 추천

X={magic(5);eye(4);rand(6)}
m=numel(X);
n=max(cellfun(@(x) size(x,1),X));
out=zeros(n,m);
for k=1:m
a=X{k}(:,1);
out(1:numel(a),k)=a;
end

댓글 수: 3

Niki
Niki 2013년 5월 8일
are you sure this works correctly ? seems like this is a loop forever :-( do you have any other idea which works faster? my laptop is working for 10 min and still not finished
Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 8일
What is the size of X?
Niki
Niki 2013년 5월 8일
[n,m]=size(peaks)
n =
8300
m =
1

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2013년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by