cell array conversion to numeric array

1 次查看(过去 30 天)
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

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-12-17
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

更多回答(1 个)

Jacob Halbrooks
Jacob Halbrooks 2013-12-17
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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by