필터 지우기
필터 지우기

How to replace matrix NaN with string from cell array

조회 수: 9 (최근 30일)
Johnny Birch
Johnny Birch 2018년 10월 1일
편집: Johnny Birch 2018년 10월 1일

I have a cell array of strings which includes some unmarked numbers and some marked with '#':

cell = {
    '# 2.537'    '1.219'      '0.457'    '0.214'    '# 0.120'    '0.245'
    '3.244'      '# 1.400'    '0.649'    '0.515'    '0.207'      '0.075'}

I want to convert the cell to a data matrix and do some calculations for instance multiply with 2:

data=str2double(cell)*2

This will leave the numbers marked by '#' with the 'NaN'. Finally, I want two things where I need some help: 1) replace the 'NaN' with the original numbers marked with '#', for instance '# 2.537' into data(1,1), and 2) convert the data matrix into a cell array of strings. The order of step 1 and 2 is not important.

the output should be:

output = {
    '# 2.537'    '2.438'      '0.914'    '0.428'    '# 0.120'    '0.490'
    '6.488'      '# 1.400'    '1.298'    '1.030'    '0.414'      '0.150'}

Thanks in advance

채택된 답변

Stephen23
Stephen23 2018년 10월 1일
편집: Stephen23 2018년 10월 1일
C = {'# 2.537','1.219','0.457','0.214','# 0.120','0.245';'3.244','# 1.400','0.649','0.515','0.207','0.075'};
mat = str2double(C);
idx = ~isnan(mat);
vec = 2*mat(idx); % your calculation
C(idx) = arrayfun(@num2str,vec,'uni',0)
  댓글 수: 1
Johnny Birch
Johnny Birch 2018년 10월 1일
편집: Johnny Birch 2018년 10월 1일
That is perfect Stephen Cobeldick! Thanks a lot.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by