Converting of the return values of a min function for code generation

1 view (last 30 days)
I have a problem with the return datatype of a MATLAB built-in min function. For example there is a matrix A(3,3,3) with values of datatype 'single'. If I use the min function, like this:
[I, K] = min(A)
I'll receive vector "I" with values with datatype 'single' and vector "K" with indices with datatype 'double'. For code generation I do not use doubles, so I need only single or integers. If I cast the vectors to int or single, the embedded coder reduce with part of the code, so it does not appear in the generated coe and there are still operations with doubles. How can I resolve the problem with return datatypes of the min function? Type casting seems not to resolve the problem.
Thanks a lot!
Best regards,
Daniel.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 5 Nov 2013
Use
K=single(K)
  1 Comment
Daniel Wolkow
Daniel Wolkow on 5 Nov 2013
Thank you. I had the same idea, but at first I was confused why embedded coder reduce this part of the operation, but it is not true. The cast operation like this:
K = single(K)
is placed in the following operation:
idx = K(opt_idx)
part of the generated code for the operation above:
start_idx = (real32_T)b_iindx[c_itmp] < 0.0F ? (real32_T)ceil
((real32_T)b_iindx[c_itmp] - 0.5F) : (real32_T)floor((real32_T)
b_iindx[c_itmp] + 0.5F);
if (start_idx < 256.0F) {
if (start_idx >= 0.0F) {
startC_idx = (uint8_T)start_idx;
} else {
startC_idx = 0U;
}
} else {
startC_idx = MAX_uint8_T;
}
But it is not the solution of the problem. MATLAB use always doubles for values and indices, but if you generate C code, the indices will be casted to int32 datatype (see C code above) and the values receive the datatype that you have set in your MATLAB code. If you know this, you can use typecast like this:
K = int32(K)
and then do your operations in vector or matrix. So the generated code is more efficient and there are no type casts anymore and no doubles, such as real_T. If you want to write efficient MATLAB code for code generation, you have to use indices of matrices or vectors with datatype int32 and nothing else.
Best regards,
Daniel.

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!