0

I am trying to convert a neural network function written in MATLAB to C function by using MATLAB Codel APP. But when I try to convert, I am getting as

cell2mat is not supported for code generation arrayfun is not supported for code generation

How can I replace these functions?

My code is as given below. Input X is a 1x2500 matrix and output is a 1x6 matrix.

Full code is aavailable in this link boneGrwNN

function Y = boneGrwNN(X)
x1_step1.ymin = -1;    
% ===== SIMULATION ========    
% Format Input Arguments
isCellX = iscell(X);
if ~isCellX, X = {X}; end;    
% Dimensions
TS = size(X,2); % timesteps
if ~isempty(X)
    Q = size(X{1},1); % samples/series
else
    Q = 0;
end    
% Allocate Outputs
Y = cell(1,TS);    
% Time loop
for ts=1:TS        
    % Input 1
    X{1,ts} = X{1,ts}';
    Xp1 = mapminmax_apply(X{1,ts},x1_step1);        
    % Layer 1
    a1 = tansig_apply(repmat(b1,1,Q) + IW1_1*Xp1);        
    % Layer 2
    a2 = softmax_apply(repmat(b2,1,Q) + LW2_1*a1);        
    % Output 1
    Y{1,ts} = a2;
    Y{1,ts} = Y{1,ts}';
end   
% Format Output Arguments
if ~isCellX, Y = cell2mat(Y); end
end

% ===== MODULE FUNCTIONS ========    
% Map Minimum and Maximum Input Processing Function
function y = mapminmax_apply(x,settings)
y = bsxfun(@minus,x,settings.xoffset);
y = bsxfun(@times,y,settings.gain);
y = bsxfun(@plus,y,settings.ymin);
end    
% Competitive Soft Transfer Function
function a = softmax_apply(n,~)
if isa(n,'gpuArray')
    a = iSoftmaxApplyGPU(n);
else
    a = iSoftmaxApplyCPU(n);
end
end
function a = iSoftmaxApplyCPU(n)
nmax = max(n,[],1);
n = bsxfun(@minus,n,nmax);
numerator = exp(n);
denominator = sum(numerator,1);
denominator(denominator == 0) = 1;
a = bsxfun(@rdivide,numerator,denominator);
end
function a = iSoftmaxApplyGPU(n)
nmax = max(n,[],1);
numerator = arrayfun(@iSoftmaxApplyGPUHelper1,n,nmax);
denominator = sum(numerator,1);
a = arrayfun(@iSoftmaxApplyGPUHelper2,numerator,denominator);
end
function numerator = iSoftmaxApplyGPUHelper1(n,nmax)
numerator = exp(n - nmax);
end
function a = iSoftmaxApplyGPUHelper2(numerator,denominator)
if (denominator == 0)
    a = numerator;
else
    a = numerator ./ denominator;
end
end

% Sigmoid Symmetric Transfer Function
function a = tansig_apply(n,~)
a = 2 ./ (1 + exp(-2*n)) - 1;
end

How can I generate C code for this function ?

1
  • If you understand what cell2mat does, and you understand the inputs you are passing it, then why can't you manually replicate the behaviour without using cell2mat itself? Commented Dec 19, 2017 at 9:53

1 Answer 1

0

You can generate standalone C code only from functions supported by code generation. If you really need C code because your environment doesn't support Matlab, then you have to manually convert unsupported functions or use coder.ceval in order to use external C code that implements the same functions.

In your example, you could replace arrayfun calls with traditional for-loops. In order to implement your own cell2mat code, just type open cell2mat so see the source code of the function and try to replicate its logics within your code.

Sign up to request clarification or add additional context in comments.

Comments

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.