3

I am generating C code from Matlab coder. I have a function as follows

function C = foo( A, B ) %#codegen
    for j=1:100,
    C = C+A(j);
    end
end

The code for this function in c generated is

void foo(float A[100],B,float* C){
    for(j=0;j<100;j++){
    *C+=A[j];
    }
}

I want the code to be efficient and generated in the following way:

void foo(float* A,B,float* C){
//here B is the length of the array
for(j=0;j<B;j++){
    *C+=*(A+j);
}
}

Do you have an idea?

2
  • 1
    I believe your MATLAB code will return error since C variable is not defined. Commented Feb 9, 2012 at 16:13
  • How do you generate the C code from the Matlab? Commented Feb 9, 2012 at 18:22

3 Answers 3

3

I don't understand the whole story, but why don't you change your matlab code to actually use B for a start and tell us what happens then, like

function C = foo( A, B ) %#codegen
    for j=1:B,
        C = C+A(j);
    end
end
Sign up to request clarification or add additional context in comments.

Comments

0

You probably need to optimize your Matlab code to be more like what you want in C. The Matlab code is accessing 100 elements in A; which is what the generated C is replicating. If you want it to only go to B then you would have to make that in Matlab.

% MATLAB
function C = foo( A, B ) %#codegen
  for j=1:B,
  C = C+A(j);
  end
end

Comments

0

My guess is that Matlab Coder expects vectorized code:

Try again on this one:

function C = foo( A, B ) %#codegen
    C = sum(A(1:100));
end

1 Comment

In the OP's example, C is scalar... it seems he wants C=sum(A(1:B));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.