MATLAB Answers

Blue
0

How to reshape nested cell arrays in a table

Asked by Blue
on 14 Aug 2019
Latest activity Commented on by Blue
on 14 Aug 2019
Hi,
How does one reshape nested cell arrays in Matlab ? Let's say I have something like this:
t = table({1:24}, {1:48}, {1:48}, {1:48});
t = reshape(t.Var3, [4, 12])
How can I reshape Var2, Var3 and Var4 into 4x12 arrays instead of 1x48 ?

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by madhan ravi
on 14 Aug 2019
Edited by madhan ravi
on 14 Aug 2019
 Accepted Answer

t(:,2:4)=varfun(@(x) {reshape(x{:},4,12)},t(:,2:4))

  1 Comment

Thank you all, much appreciated.

Sign in to comment.


Answer by the cyclist
on 14 Aug 2019
Edited by the cyclist
on 14 Aug 2019

Here is one way:
t.Var2 = {reshape(t.Var2{:},4,12)}
and analogously for Var3 and Var4.
Parsing what that is doing, from the inside out:
  • Getting at the numeric array contents, using curly brackets, because Var2 is a cell array
  • Doing the reshape
  • Putting that reshaped numeric array back into a cell array
  • Storing that new cell array into Var2 again
Seems like an odd use of a table to me, but who am I to judge?

  0 Comments

Sign in to comment.