Delete repeated values of different data types from cell array

筅森魡賤 提交于 2019-12-25 07:58:16

问题


I have a list of cell array with many repeated values, which includes strings, sample time, saturation upper limit and lower limit.

For example:

MyValues={  'Lookc_at_the_stars'
            'Lookc_how_they_shine'
            'forc_you'
            'andm_everything_they_do'
            'Theym_were_all_yellow'
            'COLDPLAY_STOP'
            'COLDPLAY_PLAY'
            'COLDPLAY_PLAY'
            'COLDPLAY_PLAY'
            'COLDPLAY_BREAK'
            'COLDPLAY_BREAK'
            'Lookc_How_they_shinefor_you'
            'its_true'
            'COLDPLAY_STOP'
            'COLDPLAY_STOP'   }

And the output what I require is:

NewMyValues = { 'Lookc_at_the_stars'
                'Lookc_how_they_shine'
                'forc_you'
                'andm_everything_they_do'
                'Theym_were_all_yellow'
                'COLDPLAY_STOP'
                'COLDPLAY_PLAY'
                'COLDPLAY_BREAK'
                'Lookc_How_they_shinefor_you'
                'its_true'  }

Since I have tried using the function unique, I am not able to get the output as it's giving me an error, saying

"Error using cell/unique
Input A must be a cell array of strings."

MyValues consists of different types of data type values.

Can someone provide me a solution or function code, that I could remove the repeated values?


回答1:


Here is a loop based solution to extract unique values of a cell array will different types:

MyValues={  'Lookc_at_the_stars',
            'Lookc_how_they_shine',
            1,
            'forc_you',
            'andm_everything_they_do',
            'Theym_were_all_yellow',
            2,
            'COLDPLAY_STOP',
            'COLDPLAY_PLAY',
            1,
            'COLDPLAY_PLAY',
            'COLDPLAY_PLAY',
            {1 2 3},
            'COLDPLAY_BREAK',
            {4 3 5},
            'COLDPLAY_BREAK',
            {1 2 3},
            'Lookc_How_they_shinefor_you',
            'its_true',
            'COLDPLAY_STOP',
            'COLDPLAY_STOP'   };
N = numel(MyValues);
idx = true(N,1);
for m = 1: N
    if idx(m)
        for n = (m+1):N
            if idx(n) && isequal(MyValues{m}, MyValues{n})
                idx(n) = false;
            end
        end
    end
end
result = MyValues(idx);

the result:

result =
{
  [1,1] = Lookc_at_the_stars
  [1,2] = Lookc_how_they_shine
  [1,3] =  1
  [1,4] = forc_you
  [1,5] = andm_everything_they_do
  [1,6] = Theym_were_all_yellow
  [1,7] =  2
  [1,8] = COLDPLAY_STOP
  [1,9] = COLDPLAY_PLAY
  [1,10] =
  {
    [1,1] =  1
    [1,2] =  2
    [1,3] =  3
  }
  [1,11] = COLDPLAY_BREAK
  [1,12] =
  {
    [1,1] =  4
    [1,2] =  3
    [1,3] =  5
  }
  [1,13] = Lookc_How_they_shinefor_you
  [1,14] = its_true
}

The function isequal can compare anything using it all values compared and duplicates removed. so result is a cell array that contains unique values.
Based on the example in the question if you want to have unique cell array of characters you can use cellfun with ischar to check if the cell value is a character array. Then use the logical index to extract them and apply unique.

unique(MyValues(cellfun(@ischar,MyValues)),'stable')

without using stable option the result will be sorted




回答2:


The cell you supplied in your question only contained strings, so unique can handle it. However, if you add ints, floats or complex numbers to it, the way to go is to convert all the cell elements to strings before calling unique. For example, I'll show you a small strcaster function

function y = strcaster(x)
    if ischar(x)
        y = x;
    elseif isreal(x)
        y = num2str(x);
    else
        if imag(x)>=0
            s = '+j';
        else
            s = '-j';
        end
        y = [num2str(real(x)),s,num2str(imag(x))];
    end
end

and then you can get the unique elements, preserving the order in which they appear in the cell doing the following:

MyValues={'Lookc_at_the_stars',...
'Lookc_how_they_shine',...
'forc_you',...
'andm_everything_they_do',...
'Theym_were_all_yellow',...
'COLDPLAY_STOP',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_BREAK',...
'COLDPLAY_BREAK',...
'Lookc_How_they_shinefor_you',...
'its_true',...
'COLDPLAY_STOP',...
'COLDPLAY_STOP',...
1,...
1.32423,...
complex(-3.,13)};
[u,i] = unique(cellfun(@(x)strcaster(x),MyValues,'uniformoutput',false),'stable');
disp(MyValues(i))

Edit

Based on your comment, it is clear that the MyValues cell contains other cells, which was not clear from your question's example. The best way to get the unique values in MyValues is still casting the contents to strings. The JSON protocol will allow you to convert almost any data type to a string, so I recommend using matlab's jsonencode function in the following way:

MyValues={'Lookc_at_the_stars',...
'Lookc_how_they_shine',...
'forc_you',...
'andm_everything_they_do',...
'Theym_were_all_yellow',...
'COLDPLAY_STOP',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_PLAY',...
'COLDPLAY_BREAK',...
'COLDPLAY_BREAK',...
'Lookc_How_they_shinefor_you',...
'its_true',...
'COLDPLAY_STOP',...
'COLDPLAY_STOP',...
1,...
1.32423,...
[1,23,4,4;5,3,2,1],...
{'bla',1324,{'12',123}},...
struct('NextTime','Provide a complete working example in the question')};
[u,i] = unique(cellfun(@(x)jsonencode(x),MyValues,'uniformoutput',false),'stable');
disp(MyValues(i))


来源:https://stackoverflow.com/questions/41243383/delete-repeated-values-of-different-data-types-from-cell-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!