Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with strcmp function in cell array

I am using cell array a with value like below

a = {'one' , 'two' ; 'four','five'};

now I am just compare the a with some string like 'two' and after then i just add another one column in a and insert string in that place

a{strcmp(a,'two'),3} ='Three' ; 

I am getting output like below

 a = 

     'one'     'two'          []
     'four'    'five'         []
         []        []    'Three'

but actually I want output like below

 a = 

     'one'     'two'     'Three'
     'four'    'five'         []

How can I do this?

like image 375
Karthick Rajan Avatar asked Jul 18 '26 20:07

Karthick Rajan


1 Answers

Have you considered using maps for your task?

%// create map
keySet =   {'one', 'two', 'three', 'four', 'five', 'six','seven','eight'};
valueSet = [1, 2, 3, 4, 5, 6, 7 ,8];
mapObj = containers.Map(keySet,valueSet);

%// data
a = {'one' , 'two' ; 'four', 'five'};

%// analyze data
Nums = cell2mat(values(mapObj,a));

%// expand data
Nums(:,3) = Nums(:,2) + 1

%// output
output = keySet(Nums)

output = 

    'one'     'two'     'three'
    'four'    'five'    'six' 
like image 137
Robert Seifert Avatar answered Jul 22 '26 02:07

Robert Seifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!