# How do you vertically concatenate the rows of a cell array consisting of vectors of numbers?

29 vues (au cours des 30 derniers jours)
L'O.G. le 30 Mar 2022
Commenté : Simon Chan le 30 Mar 2022
I want to vertically concatenate the elements of a cell array so that, for example, an array that is 3 x 9 becomes 1 x 9. How do I do that? The following concatenates everything into one dimension, which isn't what I want:
vertcat(cellarray{:});
##### 2 commentairesAfficher AucuneMasquer Aucune
the cyclist le 30 Mar 2022
What you want to do is not clear to me. Can you upload the cell array (using the paperclip icon in the INSERT section of the toolbar), or perhaps just write code here to define an smaller (like 3x2) input, and what you expect the output to be?
The best method will likely depend on whether the cell elements are numeric, strings, character arrays, etc.
L'O.G. le 30 Mar 2022
Modifié(e) : L'O.G. le 30 Mar 2022
Each element of the cell array contains an N x 1 vector where N is not necessarily the same for each vector and the elements of the vector are numbers with double precision.

Connectez-vous pour commenter.

### Réponse acceptée

per isakson le 30 Mar 2022
Example
m = magic(3);
m = [m,m,m];
cac = num2cell(m); % sample data
arrayfun( @(ix) vertcat(cac{:,ix}), [1:9], 'uni',false )
ans = 1×9 cell array
{3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double}
Is this what you look for?
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
L'O.G. le 30 Mar 2022
Modifié(e) : L'O.G. le 30 Mar 2022
Yes, beautiful! Thanks! I didn't know about arrayfun.

Connectez-vous pour commenter.

### Plus de réponses (2)

Simon Chan le 30 Mar 2022
Tru the following:
cellfun(@(x) cell2mat(x),num2cell(a,1),'uni',0)
##### 2 commentairesAfficher AucuneMasquer Aucune
L'O.G. le 30 Mar 2022
Modifié(e) : L'O.G. le 30 Mar 2022
@Simon Chan What is a?
Simon Chan le 30 Mar 2022
a is the 3x9 cell array

Connectez-vous pour commenter.

Walter Roberson le 30 Mar 2022
Output = arrayfun(@(ColIdx) vertcat(YourCell{:,ColIdx}), 1:size(YourCell,2), 'uniform', 0);
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by