필터 지우기
필터 지우기

how can I use a loop instead of cellfun

조회 수: 1 (최근 30일)
Manav Divekar
Manav Divekar 2021년 11월 23일
편집: Jan 2021년 11월 23일
function [out] = motifsupport (s,n)
% Returns indicies of found targets.
f1 = strfind(s,n);
% Returns all the not matching strings.
nomatch = cellfun(@isempty, f1);
match = length(nomatch(nomatch == 0));
out = match/length(f1);

답변 (1개)

Jan
Jan 2021년 11월 23일
편집: Jan 2021년 11월 23일
Why do you want to use a loop? To improve the speed? Then there are better methods:
  • cellfun('isempty', f1) is faster than cellfun(@isempty, f1), but it does not work with strings. Are the inputs char vectors or strings?
  • length(nomatch(nomatch == 0)): Faster: numel(s) - sum(nomatch).
  • nomatch = cellfun(@isempty, strfind(s,n)): Faster: nomatch = ~contains(s, n)
function out = motifsupport(s,n)
out = sum(contains(s, n)) / numel(s);
end
Is there still a need to use a loop?
function out = motifsupport(s, n)
count = 0;
for is = 1:numel(s)
count = count + any(strfind(s{is}, n));
end
out = count / nuzmel(s);
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by