Concatenation of columns of several cell Arrays

조회 수: 29 (최근 30일)
ErikaZ
ErikaZ 2018년 4월 25일
댓글: ErikaZ 2018년 6월 23일
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
Stephen23
Stephen23 2018년 4월 25일
편집: Stephen23 2018년 4월 25일

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

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 4월 25일
편집: Stephen23 2018년 4월 25일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by