Accessing Matlab arrays efficiently

조회 수: 6 (최근 30일)
Murat Panayirci
Murat Panayirci 2020년 11월 26일
답변: Raunak Gupta 2020년 12월 5일
Hi everyone,
I have a question regarding the efficiency of accessing arrays in matlab. Below you can see a screenshot taken from profiler. I thought it was a little bit surprising that lines 22-23 takes a fraction of lines 26-27. I realize that I am accessing more element of the array, however I would not expect the difference to be this much.
I would be glad if you could advise me on any methods or tricks which could be helpful to improve the efficiency?
thank you,
Murat

답변 (1개)

Raunak Gupta
Raunak Gupta 2020년 12월 5일
Hi Murat,
As I can understand from Line 22-23, Mposition/Mvelocity are 2-D matrix and returned VpositionA/ VvelocityA are 1-D vectors. Since in Line 26-27 the same Mposition/Mvelocity is used in array indexing but here VindexB is an 1-D array rather than scalar value i, that is why the returned MpositionB/MvelocityB will be again a 2-D array rather than 1-D vector. I assume the size of VindexB is significantly greater than 1, that is why there is scaling of 10-15 times in time taken.
Here since the size of returned variable is different (1-D vs 2-D) in both set of lines, therefore we cannot compare the time taken. In my opinion if you are not thinking of using parfor then array indexing will work fine.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by