# Converting a cell array into a matrix of stipulated number of columns and rows

3 Ansichten (letzte 30 Tage)
Vinay Killamsetty am 12 Dez. 2019
Bearbeitet: Andrei Bobrov am 12 Dez. 2019
Consider I have a cell array containing 4 cells
Each cell containd 'n' elements
Now I want to covert it into an array of size "n*4" where element of each row is take from elements of each cell
If it has tto be more clear for eg: ist row of the matrix contains 4 elements - each element is picked from 1st element of each cell.
I have used the model
for i = 1:numel(W{1})%w is the cell
wid(i,:)=double(cellfun(@(v) v(i),W));
end
But it is taking more time in matlab.So, is there any best and simple solution for this problem which can be simulated in less time?
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Alex Mcaulley am 12 Dez. 2019
Have you tried with cell2mat?

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Andrei Bobrov am 12 Dez. 2019
Bearbeitet: Andrei Bobrov am 12 Dez. 2019
Let C - your cell array.
if cells sizes are the same:
out = reshape(cat(2,C{:}),[],4);
if not the same:
out = cell2mat(cellfun(@(x)x(:),C,'un',0));
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

meghannmarie am 12 Dez. 2019
wid = [W{1}(:) W{2}(:) W{3}(:) W{4}(:)];
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

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

R2019b

### Community Treasure Hunt

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

Start Hunting!