Sorting cell for only dublicate values
Ältere Kommentare anzeigen
Hi i have a cell containing strings which are only 3 characters. I wish to sort this cell so only values that appear twice or more will remain. I thought about using a logical array, and using fun2cell, but i cant seem to get it quite right.
3 Kommentare
Image Analyst
am 25 Okt. 2021
Give a small example, like
ca = {'abc', 'aac', 'cde', 'eee'}
and tell us what the output should be. Do you want only cells 2 and 4 to be in the output cell array?
dpb
am 25 Okt. 2021
@Image Analyst, I'd think from the Q? that the output array for that sample input would be the null set; there are none that are repeated. I guess it's possible he means the letters inside the strings...
Indeed, we would need more definition to be precise.
Magnus Rasmussen
am 25 Okt. 2021
Akzeptierte Antwort
Weitere Antworten (1)
Jan
am 25 Okt. 2021
Is the input really a cell containging strings? Or a cell string? Ort a string array?
% Test data with a cell string:
data = sprintfc('%03d', randi([0, 100], 1, 100));
data = sort(data(isMultiple(data)))
function T = isMultiple(A)
[S, idx] = sort(A(:).');
if iscellstr(A)
m = [false, strcmp(S(1:nA - 1), S(2:nA))];
elseif isa(A, 'string')
m = [false, (S(1:nA - 1) == S(2:nA))];
else
error(['Jan:', mfilename, ':BadInputType'], ...
'Input type is not handled: %s', class(A));
end
ini = strfind(m, [false, true]);
m(ini) = true; % Mark 1st occurence in addition
T(idx) = m; % Restore original order
end
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
