Convert cell to matrix with mixed data types

조회 수: 18 (최근 30일)
Brian
Brian 2012년 2월 28일
I am trying to accomplish something relatively simple (most likely) but am struggling. I have a large cell array that I'd like to convert to a matrix. My cell array contains mostly numbers but a few '#N/A' where my source data was blank. I'd like to convert this cell array to a matrix and have those N/A's be converted to NaN. How can I go about doing this?
Thanks, Brian

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 2월 28일
eg
A = {[3] [4] [ 4] '#N/A'
[2] [1] '#N/A' [ 3]
[4] [2] [ 5] [ 3]
[1] [4] [ 4] [ 2]}
A(cellfun(@ischar,A)) = {NaN}
out = cell2mat(A);
A - cell array - column
i1 = cellfun(@ischar,A)
sz = cellfun('size',A(~i1),2)
A(i1) = {nan(1,sz(1))}
C = cell2mat(A)
  댓글 수: 1
Brian
Brian 2012년 3월 5일
Simple, and does exactly what I need it do.
Thanks much,
Brian

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 2월 28일
Do the sizes all line up?
You could first run your cell through a for-loop that identifies, '#N/A's and then replaces them. E.g:
C = {magic(3);1:3;'#N/A';pi*ones(1,3);'#N/A'};
for ii = numel(C):-1:1
idx(ii) = isequal(C{ii},'#N/A');
end
C(idx) = {nan(1,3)};
C2 = cell2mat(C)

카테고리

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