0

I am trying to convert a cell array with cell contents different sizes into a matrix. I have tried the following code (from a previous question):

tcell = {[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6], [1], []};  %# Sample array
maxSize = max(cellfun(@numel,tcell));    %# Get the maximum vector size
fcn = @(x) [x nan(1,maxSize-numel(x))];  %# Create an anonymous function
rmat = cellfun(fcn,tcell,'UniformOutput',false);  %# Pad each cell with NaNs
rmat = vertcat(rmat{:})                  %# Vertically concatenate cells

I get the following error code :

Dimensions of matrices being concatenated are not consistent.

Error in @(x)[x,nan(1,maxSize-numel(x))]

I think my cell array is different in content from the test example (see tell) : The content of my cell array(1x31 cell) when viewing in MATLAB is

30x1 cell    40x1 cell    37x1 cell 

Do I have to do another conversion of my cell array first ? How do I convert my cell array into the form of tcell ?

I have been searching for some time now but I am not yet familiar with all the terminology. The solution can be simple, but I don't have the knowledge yet to see it. All inputs are welcome !

Community
  • 1
  • 1
YV3003
  • 25
  • 5
  • How do you want to concatenate them? What is the size of the expected output? – Andras Deak -- Слава Україні Jan 14 '16 at 13:41
  • 1
    You have to transpose your content within the cells. The piece of code works only for line vectors. – marsei Jan 14 '16 at 13:43
  • 1
    @marsei has the solution: you either have to transpose the cells, or write your anonymous function with column vector output (`@(x)[x; nan(...)]`). – Andras Deak -- Слава Україні Jan 14 '16 at 14:02
  • Does `vertcat(tcell{:})` not work? – Dan Jan 14 '16 at 14:57
  • @Andras Deak I added the line A = cellfun(@transpose,Arad,'UniformOutput',false);. The result isn't as desired. The output is a cell array with all the elements in one row and the NaN's combined each time in a cell, so the cell content is NaN,NaN etc.. (dependent on the different in size of the previous cell contents). The output that I would like to have is a mxn matrix, with m the maximum length of the content of the cells in the cell array and n the number of cells. – YV3003 Jan 14 '16 at 16:21
  • I found the answer thanks to the input of @marsei and Andras Deak A = cellfun(@transpose,Arad,'UniformOutput',false); maxSize = max(cellfun(@numel,A)); %# Get the maximum vector size fcn = @(x) [x nan(1,maxSize-numel(x))]; %# Create an anonymous function rmat = cellfun(fcn,A,'UniformOutput',false); %# Pad each cell with NaNs rmat = horzcat(rmat{:}) ; rmat = horzcat(rmat{:}) ; rmat = reshape(rmat,maxSize, []); – YV3003 Jan 14 '16 at 16:59
  • 1
    Nice one-you can post your comment as an answer and accept it! – marsei Jan 14 '16 at 17:00

2 Answers2

1

Actually your original code was almost perfect, but for line vectors. For column vectors in a cell you missed a semicolon.

    maxSize = max(cellfun(@numel,tcell));    %# Get the maximum vector size
    fcn = @(x) [x ; nan(1,maxSize-numel(x))];  %# semicolon here                
    rmat = cellfun(fcn,tcell,'UniformOutput',false);  %# Pad each cell with NaNs
Daniel
  • 11
  • 1
0

I found the following answer thanks to the input of the commentators:

A = cellfun(@transpose,Arad,'UniformOutput',false); %transpose the cell array
maxSize = max(cellfun(@numel,A)); %# Get the maximum vector size 
fcn = @(x) [x nan(1,maxSize-numel(x))]; %# Create an anonymous function rmat = cellfun(fcn,A,'UniformOutput',false); %# Pad each cell with NaNs 
rmat = horzcat(rmat{:}) ; % concatenate horizontally 
rmat = horzcat(rmat{:}) ; % concatenate horizontally again 
rmat = reshape(rmat,maxSize, []);% reshape to m(=maxsize)x n(determined by total number of elements/number of rows(maxsize)
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
YV3003
  • 25
  • 5