필터 지우기
필터 지우기

How to convert the contents of a cell array into specific numbers?

조회 수: 2 (최근 30일)
chocho
chocho 2018년 4월 4일
댓글: chocho 2018년 4월 4일
Hi guys, I wrote this code to convert the columns 1 and 2 of my cell array into specific numbers as mentioned in the code but no value changed in column 1 and no error is shown even!.
Maybe there is a problem of indexing anyone can help me out plz?
[row_patho, col_patho]=size(Pro_patho_data);
for i=1:1:row_patho
if Pro_patho_data{i,1}>=70
Pro_patho_data{i,1}=11;
end
end
for k=1:1:row_patho
if strcmpi(Pro_patho_data{k,2},'t3b')
Pro_patho_data{k,2}=3;
end
end
Inputs:
73 t2b
59 t1c
58 t3b
78 t3b
wanted_outputs:
11 t2b
59 t1c
58 3
11 3
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 4월 4일
I suspect that the entries in column 1 are character vectors, not numeric
chocho
chocho 2018년 4월 4일
@Walter Roberson no, entries in column1 are all numeric, except some entries are in the form of '6.40E+01' ...

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

답변 (2개)

Guillaume
Guillaume 2018년 4월 4일
Pro_patho_data = {73, 't2b'; 59, 't1c'; 58, 't3b'; 78, 't3b'}; %demo data
col1 = cell2mat(Pro_patho_data(:, 1));
col1(col1 > 70) = 11;
Pro_patho_data(:, 1) = num2cell(col1);
Pro_patho_data(strcmpi(Pro_patho_data(:, 2), 't3b'), 2) = {3}
  댓글 수: 10
Walter Roberson
Walter Roberson 2018년 4월 4일
%q means to import quote-delimited strings. You would need '%f%s%f%s%f%f%f'
chocho
chocho 2018년 4월 4일
@Walter Roberson, I have updated the format and i tried my code again but nothing changed

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


Birdman
Birdman 2018년 4월 4일
편집: Birdman 2018년 4월 4일
Assuming that Inputs is a cell array:
inputs={'73','t2b';'59','t1c';'58','t3b';'78','t3b'}
wanted_outputs=regexprep(inputs,{'\<7\w*','\<t3\w*'},{'11','3'})
  댓글 수: 4
chocho
chocho 2018년 4월 4일
@Birdman friend, I got this error with your code and plz I shared my data can you have a look?
Index exceeds matrix dimensions.
Error in Pathological_data_cleaning (line 31) outputs(:,i)=regexprep(Pro_patho_data(:,i),expr(1,i),expr(2,i))
chocho
chocho 2018년 4월 4일
index exceeds matrix dimensions.
Error in Patho_data_grouping (line 13) outputs(:,i)=regexprep(inputs(:,i),expr(1,i),expr(2,i));

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by