Filter löschen
Filter löschen

build a matrix from cell

8 Ansichten (letzte 30 Tage)
Niki
Niki am 8 Mai 2013
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

Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 8 Mai 2013
X={magic(4),eye(4),rand(4)} % Example
y=cell2mat(cellfun(@(x) x(:,1),X,'un',0))
  3 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 8 Mai 2013
y=cell2mat(cellfun(@(x) x(:,1)',X,'un',0))'
Niki
Niki am 8 Mai 2013
Bearbeitet: Niki am 8 Mai 2013
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});

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 8 Mai 2013
Bearbeitet: Azzi Abdelmalek am 8 Mai 2013
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 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 8 Mai 2013
What is the size of X?
Niki
Niki am 8 Mai 2013
[n,m]=size(peaks)
n =
8300
m =
1

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by