Search code examples
matlabcell

Matlab: How to cut cells in cell array to the same length


I have a cell array with different cells.

val{1}=[1 2 3 4]
val{2}=[5 6 7 8 9 10]
val{3}=[11 12 13]
val{4}=[14 15 16 17 18 19 20]

Now I want to cut every cell that I have left 2 elements:

val{1}=[1 2]
val{2}=[5 6]
val{3}=[11 12]
val{4}=[14 15]

Thanks in advance.


Solution

  • Try this:

    val = cellfun(@(x) [x(1), x(2)],val,'UniformOutput',false);