how can i covert following variables as Indexed variables ?

조회 수: 1 (최근 30일)
Raihan Ul Islam
Raihan Ul Islam 2019년 9월 6일
댓글: Raihan Ul Islam 2019년 9월 8일
I have following function. I want to improve the execution time. Matlab profiler is showing following messgae. How can i convert 'input' and 'transformedRefVal1Rt' as indexed variables.
If 'input' is an indexed variable, performance can be improved using logical indexing instead of FIND.
If 'transformedRefVal1Rt' is an indexed variable, performance can be improved using logical indexing instead of FIND.
Here Input is 1x49457 and RtRef is 1x3 Matrix. I am also attaching the data of input,RtRef,numberOfInputData
function transformedRefVal1Rt=inputTransformGPU(input,RtRef,numberOfInputData)
[M,N]=size(RtRef);
transformedRefVal1Rt=gpuArray(zeros(numberOfInputData,N));
input(find(input>RtRef(1)))=RtRef(1);
input(find(input<RtRef(N)))=RtRef(N);
for j=1:N
transformedRefVal1Rt(find(input== RtRef(j)),j)=1;
end
for j=1:N-1
aa=find((RtRef(j)> input ) & (input>RtRef(j+1)));
transformedRefVal1Rt(aa,j+1)=(RtRef(j)-input(aa))/(RtRef(1)-RtRef(j+1));
transformedRefVal1Rt(aa,j)=1-transformedRefVal1Rt(aa,j+1);
end
end

채택된 답변

Rik
Rik 2019년 9월 6일
No need for a conversion, they already are indexed variables. Lets give a small example of what this message means:
data=1:5;
L= data<2.5; %generate logical array [true true false false false]
ind=find(L); %generate linear index [1 2]
data(ind) %returns [1 2]
data(L) %returns [1 2]
Because you skip a function call if you directly use the logical array to index you get a performance gain.
In your case you can simply remove the find calls, even without needing to edit any other part of your code.
  댓글 수: 5
Rik
Rik 2019년 9월 8일
With such a low loop count I doubt you will see much gain in vectorizing this code. I also don't really see a way to vectorize this function, although I expect it can be done.
In case your question is what I mean with vectorization:
a=1:10;
for n=1:9
if mod(a(n),3)==0
a(n)=a(n+1);
end
end
%vectorized:
b=1:10;
L1=mod(b,3)==0;
L2=[false L1(1:(end-1))];
b(L1)=b(L2);
Raihan Ul Islam
Raihan Ul Islam 2019년 9월 8일
thanks a lot for all this help

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by