Fastest way to determine if a character index is a carriage return (\n)?

37 ビュー (過去 30 日間)
Will Kinsman
Will Kinsman 2016 年 7 月 29 日
編集済み: dpb 2016 年 7 月 29 日
Hi all.
1.15 seconds per 100,000 iterations:
if regexp(text(index),'[\n]','once')
y = 1;
else
y = 0;
end
1.25 seconds per 100,000 iterations:
ret = regexp(text(index),'[\n]');
if any(ret==index)
y = 1;
else
y = 0;
end
I would think there is a faster way, but I have yet to find it. Any Suggestions??
Thanks so Much!
Will
EDIT: I acknowledge there are more than 1 type of carriage return, for simplicity lets assume the text only has one type.
  2 件のコメント
Stephen23
Stephen23 2016 年 7 月 29 日
編集済み: Stephen23 2016 年 7 月 29 日
"there are more than 1 type of carriage return,"
There exactly one carriage return character, in MATLAB represented by the escaped character \r.
Here is a table of the tab and line control characters defined in the ASCII standard:
8 \b Backspace
9 \t Horizontal Tab
10 \n Line Feed
11 \v Vertical Tab
12 \f Form Feed
13 \r Carriage Return
Several of these (and combinations thereof) have been used to indicate a newline in a text file, but a newline standard is not the same thing as a carriage return character.
Guillaume
Guillaume 2016 年 7 月 29 日
編集済み: Guillaume 2016 年 7 月 29 日
AS per dpb answer, for finding a single character, direct comparison is going to be A LOT faster than involving a regular expression engine. I just wanted to comment on the regular expression.
[] is used in regular expressions to group several characters together (i.e. match any of the characters within the brackets). When there's only one character to match, the brackets serve no purpose, so
regexp(text, '\n', once)
would have worked just as well and would avoid wondering if the fact that there's only one character in the class is a bug or not.
I doubt it would make any difference to speed.

サインインしてコメントする。

採用された回答

dpb
dpb 2016 年 7 月 29 日
y=any(text(index)==char(10));
  4 件のコメント
Will Kinsman
Will Kinsman 2016 年 7 月 29 日
hahahaaa done! Super appreciate the help. This massively sped up my program!
dpb
dpb 2016 年 7 月 29 日
編集済み: dpb 2016 年 7 月 29 日
No problem, glad to help where can... :)
Another note is that if performance is the issue, as written passing the index into the function it probably actually would make more sense to simply do the test inline instead of using the function.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 7 月 29 日
I'm not sure what 15 is (it's a "shift in" character) but, like Stephen says carriage return is 13. And you don't need char(). So it would be
crIndexes = yourText == 13;
Here's a full demo showing lots of possible ways that new lines show up in the ASCII bytes:
yourText = sprintf('abc\n def \r hij \r\n klm \n\r theEnd')
crIndexes = yourText == 13 % Find all CR
lfIndexes = yourText == 10 % Find all LF
% Find pairings with strfind():
crLF_location = strfind(yourText, [13, 10])
lfCR_location = strfind(yourText, [10, 13])
  1 件のコメント
dpb
dpb 2016 年 7 月 29 日
..."you don't need char()..."
Good catch, IA; I'll make the correction. Normally I don't do that, not sure why did this go-'round... :(

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by