Vectorizing unicode2native
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi
I am trying to remove the non-compatible letter with windows-1252 encoding. I can do this using a for-loop, but trying to speed up this algorithm if possible.
events_Description =["1:BRAZIL-ANAPOLIS";"2:BRAZIL-Barra de São Francisco";"3:BRAZIL-CAJAMAR";"4:BRAZIL-CAMAÇARI";"5:BRAZIL-CANSANÇÃO";"6:BRAZIL-CONCEIÇÃO DE FEIRA";"7:BRAZIL-CONCEIÇÃO DO JACUÃPE"];
tdata = repmat("", height( events_Description), 1);
for runi= 1: length( events_Description)
tnew = unicode2native( events_Description( runi), 'windows-1252');
tnew( ismember( tnew, [129 141 143 144 157])) = [];
tdata( runi)= native2unicode( tnew, 'windows-1252');
end
댓글 수: 0
답변 (1개)
Walter Roberson
2022년 12월 1일
%precompute
L = false(1,MaximumPermittedCharacter) ;
L(native2unicode(1:255,'windows-1252')) = true;
%if you want to disallow specific characters set them to false
%then the filtering becomes
Message = Message(L(Message));
댓글 수: 3
Walter Roberson
2022년 12월 1일
You are creating a lookup table to determine whether each character is to be permitted or not. Then you just take the input and use it to index the table. But for that to be successful, the input characters from events_description cannot have any entries with character position larger than the size of your table.
If you know ahead of time that no input character will ever be beyond (say) U+23FF (which gets you smart quotes, infinity symbol, power-off-button character), then you can
MaximumPermittedCharacter = 0x23ff;
L = false(1,MaximumPermittedCharacter) ;
L(native2unicode(1:255,'windows-1252')) = true;
to create the table.
The alternative is,
L = logical.empty;
L(native2unicode(1:255,'windows-1252')) = true;
MaximumPermittedCharacter = size(L,2);
then the lookup becomes
Message(Message > MaximumPermittedCharacter) = [];
Message = Message(L(Message));
which requries two passes over the message instead of one pass
Walter Roberson
2022년 12월 1일
Message is basically the 'events_Description'?
Message would be events_Description{runi} -- a character vector
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!