Sorting cell for only dublicate values

조회 수: 2 (최근 30일)
Magnus Rasmussen
Magnus Rasmussen 2021년 10월 25일
편집: dpb 2021년 10월 26일
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
dpb
dpb 2021년 10월 25일
@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
Magnus Rasmussen 2021년 10월 25일
Of course. I will try and give as much information as i can
BSW01_Rawtable is from the readtable function where import an excell sheet. "b" is one column from the excell sheet.
Here you see the content of b. I wish to sort it so every value appears twice or more. If there is less than 1 entry of value for example A05, i wish to have it sorted out of the list.
Hope this clarify things. Thanks for your quick replies. I know it would probably be faster to just sort it manually in the excell sheet, but i got more things of the same task and i value the learning experience. Apologies for my bad matlab language.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2021년 10월 25일
편집: dpb 2021년 10월 26일
Alternate approach to Jan's using features of categorical arrays (which the IDs logically are)
% some dummy data of the same type as the example -- convert to categorical
>> CID=categorical(sort(cellstr(strcat(char('A'+randi([0 2],10,1)),num2str(randi(5,10,1),'%02d')))));
>> CID
CID =
10×1 categorical array
A02
A03
A04
B02
B02
B02
C02
C02
C04
C04
>>
% The engine
c=categories(CID); % a list of the categories (unique list of IDs)
isG1=(countcats(CID)>1); % logical vector of those with >1 occurrences
ID=CID(ismember(CID,c(isG1))); % select those out of the total list
The above yields
>> ID
ID =
7×1 categorical array
B02
B02
B02
C02
C02
C04
C04
>>
which can be seen by inspection to be the duplicates in the original list.
  댓글 수: 1
Magnus Rasmussen
Magnus Rasmussen 2021년 10월 26일
Yes this is perfect. Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2021년 10월 25일
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

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by