cell array conversion to numeric array

5 Ansichten (letzte 30 Tage)
Dany
Dany am 17 Dez. 2013
Kommentiert: Dany am 17 Dez. 2013
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

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 17 Dez. 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

Weitere Antworten (1)

Jacob Halbrooks
Jacob Halbrooks am 17 Dez. 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

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by