Efficient way to chain element-wise operations for code generation?

조회 수: 10 (최근 30일)
Gershom Agim
Gershom Agim 2019년 2월 5일
편집: Gershom Agim 2019년 2월 6일
Hi, I have functions that chain elementwise array operations eg:
function y = Foo(X)
y = 20*log10(abs(X));
end
I want to use Matlab Coder to make C code out of this.
I inspect the C code generated.
It performs all the sub-expressions as their own separate loops like:
//Pseudocode:
for (loop over array){
Y = abs(X)
}
for (loop over array){
Y = log10(Y)
}
for (loop over array){
Y = 20.0f * Y
}
is there any way to fuse all these into 1 loop (as shown below) without manually editting the generated code:
for (loop over array){
Y = 20.0f * log10(abs(X))
}
I'm hoping for something like an elementwise functor mapped across an array like arrayfun, but I haven't been able to get anything working.

답변 (1개)

Gershom Agim
Gershom Agim 2019년 2월 6일
편집: Gershom Agim 2019년 2월 6일
My (really inelegant) solution to this is to create the result array outside the function, and pass that in as an argument, then explicitly write out the for-loops.
Eg:
function Y = Foo(X,Y)
% Assert sizes here so codegen knows
for i=1:size(X,1)
for j=1:size(X,2)
Y(i,j) = 20*log10(abs(X(i,j)));
end
end
end
Is there a better way of doing this?

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by