필터 지우기
필터 지우기

Does Matlab automatically vectorize for loops?

조회 수: 3 (최근 30일)
FM
FM 2024년 6월 12일
편집: FM 2024년 6월 14일
I currently have Python code that takes 1.5 hours to calculate 450+ million Levenshtein distances between 30,011 text labels of no more than 20 characters each. Currently migrating it to Matlab to see the JiT compilation allows optimizations of flow control. The Python implementation uses Anaconda's CPython, so no JiT, and there is a distinct possibility that Matlab could be much faster.
Years ago, I recall reading that Matlab automatically vectorizes for loops. I cannot find any such mentions now. Is this just assumed to be part of the JiT compilation?
There will be 2 levels of for-loops, one nested within the other, to iterate over matrix rows and columns. Each iteration will call editDistance, so I don't know if that will prevent vectorization, even if automated vectorization was available.

채택된 답변

Walter Roberson
Walter Roberson 2024년 6월 13일
Years ago, I recall reading that Matlab automatically vectorizes for loops. I cannot find any such mentions now. Is this just assumed to be part of the JiT compilation?
That is not done. (Or if it is done, it is in fairly narrow circumstances.)

추가 답변 (1개)

Steven Lord
Steven Lord 2024년 6월 12일
In general we don't discuss what the JIT does internally as we don't want users to try to code against that always-moving target. [What if you write your code in a somewhat "unnatural" way to avoid some JIT limitations and the next release we remove those limitations and make the more "natural" implementation much faster? Would you ever notice the change?]
Searching the online documentation for the term "Levenshtein" found four functions, two each in Text Analytics Toolbox and Computer Vision Toolbox, whose reference pages include that term. One or more of those may be useful to you. There are also a few hits in MATLAB Answers and in the File Exchange, if you look at the search results without the scoping to just functions in MathWorks products.
  댓글 수: 4
Paul
Paul 2024년 6월 14일
editDistance is vectorized from a user interface perspective, but it's not really. As best I can tell, it basically uses arrayfun under the hood to call itself in a loop for all of the pairs of scalars in the input. And on each call it does argument validation and various other checks that really only need to be done once. I haven't done any testing, but I suspect editDistance could be modified to be (much?) more efficient.
FM
FM 2024년 6월 14일
편집: FM 2024년 6월 14일
@Paul: I suspected as much. Thanks.
@Christopher: I'm actually trying to explore the clustering of the 30,011 labels to see which can be considered typos or embellishments of each other. At the outset, I don't have specific words for which I'm searching for possible matches witin a certain edit distance. After exploring the clustering, I might be able to set such thresholds.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by