简体   繁体   中英

Delete string and double redundancy in cell of matlab

I have a cell array with two columns. One consists of strings and the other of doubles. I would like to delete all rows, which are redundant.

b = cell(4,2);
b{1,1} = 'a';
b{1,2} = 2;
b{2,1} = 'a';
b{2,2} = 1;
b{3,1} = 'b';
b{3,2} = 1;
b{4,1} = 'a';
b{4,2} = 2;

So that in the list above the row a 2 will be deleted. Also a sorting would be nice. I found the 'unique' function which tells me that it just works for all string cells. Is there another function for mixed cells?

Best regards Manuel

This is the answer thanks to the help I got. Convert the doubles to strings, merge the two strings, unique, separate again and convert the string to double:

b = cell(4,2);
b{1,1} = 'a';
b{1,2} = 2;
b{2,1} = 'a';
b{2,2} = 1;
b{3,1} = 'b';
b{3,2} = 1;
b{4,1} = 'a';
b{4,2} = 2;
b

btemp = {};
for i = 1:size(b)   
    [~,columns]=size(btemp);
    btemp{columns+1} = strcat(b{i,1},sprintf('%.3f',b{i,2}));
end
btemp = unique(btemp);
[~,columns]=size(btemp);

b2 = cell(columns,2);
for j = 1:columns
    b2{j,1} = btemp{j}(1);
    b2{j,2} = str2double(btemp{j}(2:end));
end

b2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM