Allow arbitrarily sized inputs for codegen without infinite bounds on array size
4 次查看(过去 30 天)
显示 更早的评论
Hello, I am playing with the code generator (R2014b) and I am trying to allow an input to be an arbitrarily sized array with a fixed number of dimensions. This code (designed to allow me to test the speed of a random loop) seems to work:
function outVal = myFuncTest(inVal) %#codegen
assert(isa(inVal,'double'));
assert(all(size(inVal) <= [inf inf]));
outVal = zeros(100,1);
for ii = 1:100
for jj = 1:numel(inVal)
outVal(ii) = 3.*myFunc(inVal(jj)).*(ii./100);
end
end
end
function outVal = myFunc(x)
outVal = (x.^2)/2;
end
end
The key to make it work seems to be this line:
assert(all(size(inVal) <= [inf inf]));
but it doesn't seem right to me to have to use infs as an upper limit to array size. I have tried to use the coder.varsize option by putting:
coder.varsize('inVal',[]);
as the first line of the function instead, but when I do, I get an error saying that:
??? Size of inVal cannot be modified as the variable inVal is already locked.
Dimension 1 cannot be made dynamic: it has previously been set to have fixed size 1.
If I leave out both statements, the code compiles but then gives an error at runtime, since the coder seems to explicitly need variable size to be given:
myFuncTest_mex(ones(100))
MATLAB expression 'inVal' is not of the correct size: expected [1x1] found [100x100].
I would be grateful if someone could point out to me the correct way of achieving this, or let me know if there is any practical problem with using the infinite bounds on variable size.
0 个评论
采纳的回答
Ryan Livingston
2018-3-19
Using Inf is the standard way to specify unbounded array sizes for Coder. You can use that with assert or with varsize: coder.varsize('inVal',[Inf,Inf]);
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!