matlab - Replace a string in cell array into 1x3 numeric cell array -
cell array data below:
data= 'a' [0.006] 'b' 'c' [3.443] 'c'
i convert character in first column in 1x3 vector, mean
- 'a' replace [0] [0] [0],
- 'c' replace [0] [1] [0]..
the result be
[0] [0] [0] [0.006] 'b' [0] [1] [0] [3.443] 'c'
the code tried below:
b=data(1:end,1); b=regexprep(b,'c','[0 0 0]'); b=regexprep(b,'a','[0 1 0]');
the result show me
b= '[0 0 0]' '[0 1 0]'
which wrong, each character not change 1x3 array...please help...
since did not specify rule convert letters numbers, assumed want replace a
000
, b
001
, ..., h
111
(ie numbers 0 7 in binary, corresponding letters h).
in case want go z, code below can changed.
%# data cell array data = { 'a' [0.006] 'b' 'c' [3.443] 'c' }; %# compute binary numbers equivalent letters h binary = num2cell(dec2bin(0:7)-'0'); %# use 0:25 go z %# convert letters in row indices in above cell array "binary" idx = cellfun(@(c) c-'a'+1, upper(data(:,1))); %# replace first column, , build new data newdata = [binary(idx,:) data(:,2:end)]
the result:
newdata = [0] [0] [0] [0.006] 'b' [0] [1] [0] [3.443] 'c'
Comments
Post a Comment