More efficient returning string position in cell string array
4 次查看(过去 30 天)
显示 更早的评论
Given an array such as
>> whos endtxt
Name Size Bytes Class Attributes
endtxt 137x2 22466 cell
Surely there is a (much) less verbose way to return the location of a specific string within the array than
>> find(~cellfun('isempty',strfind(endtxt(:,2),'FULLER')))
ans =
46
>>
I've whiffed on an efficient way to do something useful with the cell array of a zillion empty cells excepting for the one(s) of interest...the above does (finally!) work, but surely????
2 个评论
回答(3 个)
Guillaume
2014-10-8
Assuming that the string you're looking for ('FULLER') is the exact match for one of the string in the cell array (and not just a substring), then
find(ismember(endtxt(:, 2), 'FULLER'))
matt dash
2014-10-8
If i'm understanding correctly: [junk,answer] = ismember('FULLER',endtxt(:,2))
2 个评论
matt dash
2014-10-8
Well, here is an option with even more rigamarole, but it is faster if that matters. Basically make a copy of your text that is not a cell array, and cross reference it with a vector indicating where row breaks occur. For potentially even more speed you could remove the find entirely by keeping a vector of row indices for every character in the text (if memory is not an issue)
1) use cellfun(@length,<cells>) to get the length of each cell, then cumsum this to get the start index of each line (pre-pend a 0 at the beginning) 2) convert the cell arrays to one long string with [<cells>{:}] 3) now just use strfind on this one string to get the index 4) cross reference this with the index vector from (1) to see which line it begins in
Ridiculous, but on my computer it is 10-30x faster than find(~cellfun('isempty',strfind(lines,teststr)))
and seems to get faster for larger amounts of text.
code:
fid = fopen('book.txt','r'); %some long text file
teststr='Lampsacus' %some word in it
%read text file:
tline=fgetl(fid);
lines={};
while ischar(tline)
lines{end+1}=tline;
tline=fgetl(fid);
end
%method 1:
q=cellfun(@length,lines);
starts = [0 cumsum(q)];
alltxt=[lines{:}];
tic
a=strfind(alltxt,teststr);
for i = numel(a):-1:1
idx(i)=find(starts<=a(i),1,'last');
end
toc
%method 2:
tic
find(~cellfun('isempty',strfind(lines,teststr)));
toc
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!