2

I have a 2 dimensional cell of tables, B, in Matlab, i.e.:

A = table(normrnd(0,1,5,1),normrnd(0,1,5,1),normrnd(0,1,5,1));

B = {A,A,A,A;A,A,A,A}

B = 

[5x3 table]    [5x3 table]    [5x3 table]    [5x3 table]
[5x3 table]    [5x3 table]    [5x3 table]    [5x3 table]

I would like to concatenate the tables in the 1st dimension of the cell array (the 2 rows), but keep the cell structure in the other dimension. Thus, I would like to have the following:

{cat(1,B{:,1}),cat(1,B{:,2}),cat(1,B{:,3}),cat(1,B{:,3})}

ans = 

[10x3 table]    [10x3 table]    [10x3 table]    [10x3 table]

However, since my actual cell array has much more than 2 rows and 4 columns, this is not a suitable solution. I have tried using cat and vertcat, but I can't get them to not concatenate in the second dimension. Using ´cat´, I get:

cat(1,B{:})

ans = 

[40x3 table]

Any ideas?

Thanks!

1 Answer 1

1

Try this:

result = cellfun(@(a,b) cat(1,a,b),B(1,:),B(2,:),'UniformOutput',false);

The output:

result = 

    [10x3 table]    [10x3 table]    [10x3 table]    [10x3 table]

(We need to set UniformOuput to false because the contents of the output cells are not scalars.)


The contents of one of the cells in my case:

result{1}

ans = 

      Var1       Var2        Var3  
    ________    _______    ________

    -0.20497     0.6715      1.0347
    -0.12414    -1.2075     0.72689
      1.4897    0.71724    -0.30344
       1.409     1.6302     0.29387
      1.4172    0.48889    -0.78728
    -0.20497     0.6715      1.0347
    -0.12414    -1.2075     0.72689
      1.4897    0.71724    -0.30344
       1.409     1.6302     0.29387
      1.4172    0.48889    -0.78728
Sign up to request clarification or add additional context in comments.

4 Comments

I should have mentioned this, but my real cell-array also have many rows, so I would need a solution where I don't need to add a cell-array for each row (B(1,:),B(2,:),...).
@Mace Off the top of my head I can't think of a solution without using for loops. I'll comment again if I come up with anything.
@Mace What type of data are in your tables? If it's all numerical we could do something with cell2mat and mat2cell, but I suspect there are things like variable-length strings too?
Yes, there will be both strings and numeric data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.