3

I'm trying to generate a mex code file from the .m file using Matlab coder. The code for which is

function [result,x]=tesrank(A,x)

result = [];
n = x;
for col= 1:n
   result = [result, sum(A==col, 2)];
end

For fixed size, I'm able to get it using

codegen tesrank -args {zeros(2,3), zeros(1)}
% Here size(A)=2x3 and size(x)=1x1

How do I do it without limiting the size of A and x?

Zero
  • 74,117
  • 18
  • 147
  • 154
  • When I try to execute code generation for this function, it fails. Could you verify? – Lokesh A. R. Dec 02 '13 at 14:56
  • You can do this kind of coding for arbitrary sizes of A and x as long as you give an upper bound to their sizes. See documentation pointed to by user2987828. – Navan Dec 05 '13 at 15:15

3 Answers3

1

You do not have to limit the size of the array A.

Check this example (using Matlab 2014a):

codegen('funcAccumarray1D_max.m', ...
    '-report', ...
    '-args', {coder.typeof(double(0), [Inf 1]), ...
              coder.typeof(double(0), [Inf 1])}, ...
    '-o', 'funcAccumarray1D_max')

for this function:

function [ outs ] = funcAccumarray1D_max(subs, vals, sz) 
%FUNCACCUMARRAY1D_MAX Construct an array by accumulation using 'max'
%#codegen
outs = NaN(sz, 1, 'like', vals);
for ix=1:size(subs,1)
  sub = subs(ix);
  outs(sub,1) = max(outs(sub,1), vals(ix,1));
end
end
0

For building, the function coder.typeof is your friend here. To do what you want, which is to have variables that allow unbounded dimensions, you would declare your input arguments this way:

codegen tesrank -args {coder.typeof(0, [Inf, Inf]), coder.typeof(0, [1, Inf])}

In my example, A is completely unbounded in two dimensions (you can have more than two, just increase the length of the size array to typeof) and x is bounded in the first dimension to a size of 1 exactly, but unbounded in the second dimension. Looking at your code, you may not want x unbounded, but if you do, that's how you do it.

There are more capabilities of coder.typeof you could explore.

Tony
  • 949
  • 5
  • 20
-1

You can't, according to page p7-15 and p25-24 of Mathworks' documentation.

user2987828
  • 1,116
  • 1
  • 10
  • 33