Matlab cell array to string vector - unique -
going nuts cell array, because can't rid of it... however, easy 1 guys out here.
so here why: have dataset (data) contains 2 variables: (numbers) , b (cell array). unfortunately can't reconstruct problem nevertheless imported table looks this:
data=dataset; data.a = [1;1;3;3;3]; data.b = ['a';'a';'buu';'buu';'a'];
where data.b of type 5x1 cell can't reconstruct
all want unique rows like
ans= [1 a;3 buu;3 a] result should in dataset or 2 vectors rows equivalent.
but unique([dataa datab],'rows') can't handle cell arrays , can't find anywhere in www how simple convert cell array b vector of strings (does exist?).
cell2mat() didn't work me, because of different word length ('a' vs 'buu').
though, 2 things love learn: making 5x1 cell string vector , find unique rows out of numbers , strings (or cells).
thank much!
cheers dominik
the problem a
, b
fields of different type. although concatenated cell array, unique
can't handle that. general trick cases "translate" elements of each field (column) unique identifiers, i.e. numbers. translation can done applying unique
each field separately , getting third output. obtained identifiers can concatenated matrix, each row of matrix "composite identifier". finally, unique
'rows'
option can applied matrix.
so, in case:
[~, ~, ka] = unique(data.a); [~, ~, kb] = unique(data.b); [~, jr] = unique([ka kb], 'rows');
now build result (same format data
)
result.a = data.a(jr); result.b = data.b(jr);
or (2d cell array)
result = cat(2, mat2cell(data.a(jr), ones(1,numel(jr))), data.b(jr));
Comments
Post a Comment