cell array conversion to numeric array

4 views (last 30 days)
hi, i have a cell array that contains numbers and im trying to convert it to numeric array by using the 'cell2mat' command. but i keep getting error:
**** Error using cat Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 84) m{n} = cat(1,c{:,n}); ********
what does it mean? why is it happening? and how can i fix it?
thank you

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 17 Dec 2013
If you have
v={[1 2 3] , [ 2 5] , 4}
w=cell2mat(v) % horizontal concatenation is possible
v={[1 2], [3 ; 5]}
cell2mat(v) % is not possible because you can't concatenate the two vectors

More Answers (1)

Jacob Halbrooks
Jacob Halbrooks on 17 Dec 2013
My guess is that your cell array is 2D but contains empty values so that when CELL2MAT attempts to take each cell row and create a numeric row, the empty "disappears" and the rows end up having different number of elements. The attempt to concatenate them into a 2D array then fails. Here is a simple example:
>> c = {1 2 []; 3 4 5};
>> cell2mat(c)
Error using cat
Dimensions of matrices being concatenated are not consistent.
If this is indeed the case, you could consider using code like this below to identify the empties in your cell array and replace them with a value (here, NaN):
>> emptymask = cellfun(@(x)isempty(x), c);
>> c{emptymask} = NaN;
>> cell2mat(c)
ans =
1 2 NaN
3 4 5

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!