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
  1 件のコメント
ErikaZ
ErikaZ 2018 年 6 月 23 日
Thank you, I forgot to reply.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by