필터 지우기
필터 지우기

cell array conversion to numeric array

조회 수: 5 (최근 30일)
Dany
Dany 2013년 12월 17일
댓글: Dany 2013년 12월 17일
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