Vectorizing unicode2native
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Walter Roberson
le 1 Déc 2022
%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 commentaires
Walter Roberson
le 1 Déc 2022
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
le 1 Déc 2022
Message is basically the 'events_Description'?
Message would be events_Description{runi} -- a character vector
Voir également
Catégories
En savoir plus sur String Parsing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!