Filter löschen
Filter löschen

combine cells with different size

18 Ansichten (letzte 30 Tage)
Alberto Acri
Alberto Acri am 17 Jun. 2023
Beantwortet: Stephen23 am 17 Jun. 2023
I have several different cells. How can I combine them?
I have tried it this way.
a = {'ball' , 'cake' , 'ice'};
b = {'home'};
c = {'car','moto'};
d = {'money'};
e = cell(4,3);
e(1,:) = a;
e(2,1) = b;
e(3,1:2) = c;
e(4,1) = d;
but I would like to know if it can be done in a more general way because I don't know initially the size of each cell a, b, c.
Something like this:
a = {'ball' , 'cake' , 'ice'};
b = {'home'};
c = {'car','moto'};
d = {'money'};
e = cell(4,3);
e(1,1:end) = a;
e(2,1:end) = b;
e(3,1:end) = c;
e(4,1:end) = d;

Akzeptierte Antwort

Stephen23
Stephen23 am 17 Jun. 2023
a = {'ball' , 'cake' , 'ice'};
b = {'home'};
c = {'car','moto'};
d = {'money'};
e = cell(4,0);
e(1,1:numel(a)) = a;
e(2,1:numel(b)) = b;
e(3,1:numel(c)) = c;
e(4,1:numel(d)) = d
e = 4×3 cell array
{'ball' } {'cake' } {'ice' } {'home' } {0×0 double} {0×0 double} {'car' } {'moto' } {0×0 double} {'money'} {0×0 double} {0×0 double}
"I would like to know if it can be done in a more general way..."
That will not be easy, because you split the data up into lots of separate variables. An efficient more general approach requires them to be kept together.

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by