Concatenation of columns of several cell Arrays

28 Ansichten (letzte 30 Tage)
ErikaZ
ErikaZ am 25 Apr. 2018
Kommentiert: ErikaZ am 23 Jun. 2018
I am trying to concatenate several columns of one cell array with several columns of another cell array.
I have A = 6xn, where n can be any number between 1&9.
I have B = 6xm,where m can be any number between 1&9
I need to create a cell array C = 6x1
For example:
A =
472x2 double 471x2 double 488x2 double 496x2 double 494x2 double 495x2 double
472x2 double 471x2 double 488x2 double 496x2 double 494x2 double 495x2 double
472x2 double 471x2 double 488x2 double 496x2 double 494x2 double 495x2 double
472x2 double 471x2 double 488x2 double 496x2 double 494x2 double 495x2 double
472x2 double 471x2 double 488x2 double 496x2 double 494x2 double 495x2 double
472x2 double 471x2 double 488x2 double 496x2 double 494x2 double 495x2 double
B=
460x2 double 509x2 double 503x2 double 503x2 double 488x2 double 490x2 double 488x2 double 503x2 double 518x2 double
460x2 double 509x2 double 503x2 double 503x2 double 488x2 double 490x2 double 488x2 double 503x2 double 518x2 double
460x2 double 509x2 double 503x2 double 503x2 double 488x2 double 490x2 double 488x2 double 503x2 double 518x2 double
460x2 double 509x2 double 503x2 double 503x2 double 488x2 double 490x2 double 488x2 double 503x2 double 518x2 double
460x2 double 509x2 double 503x2 double 503x2 double 488x2 double 490x2 double 488x2 double 503x2 double 518x2 double
460x2 double 509x2 double 503x2 double 503x2 double 488x2 double 490x2 double 488x2 double 503x2 double 518x2 double
The result, for each row, of concatenating all columns of A and the first 4 columns of B will be:
C=
4891x2 double
4891x2 double
4891x2 double
4891x2 double
4891x2 double
4891x2 double
I can concatenate all columns of A for each row with:
for i=1:6
C{i,1}=cat(1, A{i,:});
end
I want to avoid several for loops and the creation of intermediate variables, so I tried:
for i=1:6
C{i,1}=cat(1, A{i,:}, B(i,[1:num_extra_trials_w]));end
for i=1:6
C{i,1}=cat(1, A{i,:}, cat(1,B(i,[1:num_extra_trials_w])));end
for i=1:6
C{i,1}=cat(1, A{i,:}, {cat(1,B(i,[1:num_extra_trials_w]))});end
But none are creating a 6x1 cell array with 4891x2 double in each. What would be the proper syntax?
Thank you for your help!
  1 Kommentar
Stephen23
Stephen23 am 25 Apr. 2018
Bearbeitet: Stephen23 am 25 Apr. 2018

The square brackets here

[1:num_extra_trials_w]

are totally superfluous: you are not concatenating anything, so why use the concatenation operator? The colon operator already produces a vector, so what does [] do? Answer: nothing. See:

https://www.mathworks.com/matlabcentral/answers/35676-why-not-use-square-brackets

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Apr. 2018
Bearbeitet: Stephen23 am 25 Apr. 2018
You used the wrong brackets. You used curly braces to get the contents of the cells of A, but not with B. All you need is this:
C{i,1} = cat(1, A{i,:}, B{i,1:num_extra_trials_w});
^ ^ curly braces!
I even tried it myself with some random data:
Ar = [472,471,488,496,494,495];
Br = [460,509,503,503,488,490];
fun = @(r)rand(r,2);
A = arrayfun(fun,repmat(Ar,6,1),'uni',0);
B = arrayfun(fun,repmat(Br,6,1),'uni',0);
C = cell(6,1);
for k = 1:6
C{k,1} = cat(1, A{k,:}, B{k,1:4});
end
cellfun('size',C,1)
which shows the number of rows of the arrays in C:
ans =
4891
4891
4891
4891
4891
4891

Weitere Antworten (0)

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