cell array conversion to numeric array
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
0 Comments
More Answers (1)
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
See Also
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!