Insert/Concatenate cell array into nested cell array (by column and repeat in each row)

1 view (last 30 days)
I have CA a 1x3 cell array with {3x13 cell} {3x11 cell} {3x11 cell}. Each nested cell have 2x# double arrays.
Example CA{1} =
3×13 cell array
Columns 1 through 7
[2×153 double] [2×154 double] [2×156 double] [2×154 double] [2×156 double] [2×155 double] [2×154 double]
[2×153 double] [2×154 double] [2×156 double] [2×154 double] [2×156 double] [2×155 double] [2×154 double]
[2×153 double] [2×154 double] [2×156 double] [2×154 double] [2×156 double] [2×155 double] [2×154 double]
Columns 8 through 13
[2×151 double] [2×152 double] [2×152 double] [2×154 double] [2×154 double] [2×161 double]
[2×151 double] [2×152 double] [2×152 double] [2×154 double] [2×154 double] [2×161 double]
[2×151 double] [2×152 double] [2×152 double] [2×154 double] [2×154 double] [2×161 double]
I also have 3 individual cell arrays A {1x13 cell}, B {1x11 cell}, C{1x11 cell}. Each cell have 3x# double arrays.
Example A =
1×13 cell array
Columns 1 through 7
[3×153 double] [3×154 double] [3×156 double] [3×154 double] [3×156 double] [3×155 double] [3×154 double]
Columns 8 through 13
[3×151 double] [3×152 double] [3×152 double] [3×154 double] [3×154 double] [3×161 double]
I need to add/insert/concatenate A into each row of CA{1} to create:
CA_5{1} =
Columns 1 through 7
[5×153 double] [5×154 double] [5×156 double] [5×154 double] [5×156 double] [5×155 double] [5×154 double]
[5×153 double] [5×154 double] [5×156 double] [5×154 double] [5×156 double] [5×155 double] [5×154 double]
[5×153 double] [5×154 double] [5×156 double] [5×154 double] [5×156 double] [5×155 double] [5×154 double]
Columns 8 through 13
[5×151 double] [5×152 double] [5×154 double] [5×154 double] [5×161 double]
[5×151 double] [5×152 double] [5×154 double] [5×154 double] [5×161 double]
[5×151 double] [5×152 double] [5×154 double] [5×154 double] [5×161 double]
B corresponds to CA{2} and C to CA{3}.
I tried to use cellfun and arrayfun but I cannot get to re-concatenate into each row.
cellfun(@(x,y) [x;y], CA{1}, A,'uni',1);
Thanks

Accepted Answer

Voss
Voss on 2 Mar 2023
load SampleData
CA{:} % original
ans = 3×13 cell array
{2×153 double} {2×154 double} {2×156 double} {2×154 double} {2×156 double} {2×155 double} {2×154 double} {2×151 double} {2×152 double} {2×152 double} {2×154 double} {2×154 double} {2×161 double} {2×153 double} {2×154 double} {2×156 double} {2×154 double} {2×156 double} {2×155 double} {2×154 double} {2×151 double} {2×152 double} {2×152 double} {2×154 double} {2×154 double} {2×161 double} {2×153 double} {2×154 double} {2×156 double} {2×154 double} {2×156 double} {2×155 double} {2×154 double} {2×151 double} {2×152 double} {2×152 double} {2×154 double} {2×154 double} {2×161 double}
ans = 3×11 cell array
{2×415 double} {2×408 double} {2×428 double} {2×422 double} {2×438 double} {2×429 double} {2×438 double} {2×428 double} {2×414 double} {2×418 double} {2×432 double} {2×415 double} {2×408 double} {2×428 double} {2×422 double} {2×438 double} {2×429 double} {2×438 double} {2×428 double} {2×414 double} {2×418 double} {2×432 double} {2×415 double} {2×408 double} {2×428 double} {2×422 double} {2×438 double} {2×429 double} {2×438 double} {2×428 double} {2×414 double} {2×418 double} {2×432 double}
ans = 3×11 cell array
{2×441 double} {2×437 double} {2×413 double} {2×417 double} {2×444 double} {2×436 double} {2×421 double} {2×441 double} {2×428 double} {2×417 double} {2×422 double} {2×441 double} {2×437 double} {2×413 double} {2×417 double} {2×444 double} {2×436 double} {2×421 double} {2×441 double} {2×428 double} {2×417 double} {2×422 double} {2×441 double} {2×437 double} {2×413 double} {2×417 double} {2×444 double} {2×436 double} {2×421 double} {2×441 double} {2×428 double} {2×417 double} {2×422 double}
AA = repmat(A,size(CA{1},1),1);
BB = repmat(B,size(CA{2},1),1);
CC = repmat(C,size(CA{3},1),1);
CA_5 = { ...
cellfun(@(x,y) [x;y], CA{1}, AA,'uni',0) ...
cellfun(@(x,y) [x;y], CA{2}, BB,'uni',0) ...
cellfun(@(x,y) [x;y], CA{3}, CC,'uni',0) ...
};
CA_5{:} % new
ans = 3×13 cell array
{5×153 double} {5×154 double} {5×156 double} {5×154 double} {5×156 double} {5×155 double} {5×154 double} {5×151 double} {5×152 double} {5×152 double} {5×154 double} {5×154 double} {5×161 double} {5×153 double} {5×154 double} {5×156 double} {5×154 double} {5×156 double} {5×155 double} {5×154 double} {5×151 double} {5×152 double} {5×152 double} {5×154 double} {5×154 double} {5×161 double} {5×153 double} {5×154 double} {5×156 double} {5×154 double} {5×156 double} {5×155 double} {5×154 double} {5×151 double} {5×152 double} {5×152 double} {5×154 double} {5×154 double} {5×161 double}
ans = 3×11 cell array
{5×415 double} {5×408 double} {5×428 double} {5×422 double} {5×438 double} {5×429 double} {5×438 double} {5×428 double} {5×414 double} {5×418 double} {5×432 double} {5×415 double} {5×408 double} {5×428 double} {5×422 double} {5×438 double} {5×429 double} {5×438 double} {5×428 double} {5×414 double} {5×418 double} {5×432 double} {5×415 double} {5×408 double} {5×428 double} {5×422 double} {5×438 double} {5×429 double} {5×438 double} {5×428 double} {5×414 double} {5×418 double} {5×432 double}
ans = 3×11 cell array
{5×441 double} {5×437 double} {5×413 double} {5×417 double} {5×444 double} {5×436 double} {5×421 double} {5×441 double} {5×428 double} {5×417 double} {5×422 double} {5×441 double} {5×437 double} {5×413 double} {5×417 double} {5×444 double} {5×436 double} {5×421 double} {5×441 double} {5×428 double} {5×417 double} {5×422 double} {5×441 double} {5×437 double} {5×413 double} {5×417 double} {5×444 double} {5×436 double} {5×421 double} {5×441 double} {5×428 double} {5×417 double} {5×422 double}

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 2 Mar 2023
Edited: Dyuman Joshi on 2 Mar 2023
Change 'uniformoutput' to 0
CA{1}=cell(1,13);
A=cell(1,13);
for k=1:13
r=randi([2 4]);
%random data to show concatenation
A{k}=rand(r,156-k);
CA{1}{k}=rand(r,156-k);
end
CA{1}
ans = 1×13 cell array
{3×155 double} {4×154 double} {2×153 double} {4×152 double} {2×151 double} {3×150 double} {3×149 double} {3×148 double} {4×147 double} {2×146 double} {4×145 double} {3×144 double} {2×143 double}
A
A = 1×13 cell array
{3×155 double} {4×154 double} {2×153 double} {4×152 double} {2×151 double} {3×150 double} {3×149 double} {3×148 double} {4×147 double} {2×146 double} {4×145 double} {3×144 double} {2×143 double}
%assign it accordingly
out{1} = cellfun(@(x,y) [x;y], CA{1}, A, 'uni', 0);
out{1}
ans = 1×13 cell array
{6×155 double} {8×154 double} {4×153 double} {8×152 double} {4×151 double} {6×150 double} {6×149 double} {6×148 double} {8×147 double} {4×146 double} {8×145 double} {6×144 double} {4×143 double}
%similarly for B and C as well
  3 Comments

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!