Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simulink logical MxArray array

320 views
Skip to first unread message

Tan Biru

unread,
Sep 1, 2014, 4:10:11 PM9/1/14
to


'function [ index] = SoftMax( Q ) %UNTITLED Summary of this function goes here % Detailed explanation goes here

coder.extrinsic('softmax');

Temperature = 0.5; terms = Q/Temperature;

Prob = softmax (terms);

cummu = cumsum (Prob);

randm = rand();

index= sum(cummu <= randm) +1;'

Dear MATLAB community,

I am using this code on MATLAB Function block in Simulink. 'Q' is an array which made 'cummu' an array too. Running this function at MATLAB command windows doesn't have problems, but running in Simulink returns "Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions." for 'index= sum(cummu <= randm) +1;'

I have also tried

'for i = 1:numel(Q) if(randm >= cump(i)) action = action +1; end end'

but it returns "Subscripting into an mxArray is not supported. ... ... cummu (i)... ..."

I am stuck on this, please help. Thank you!

Tan Biru

Ryan Livingston

unread,
Sep 2, 2014, 9:29:08 AM9/2/14
to
When using the output of an extrinsic function, it may often be necessary to preinitialize the variable to which it is assigned. Doing so is what tells the code generation software about the type, size and complexity of the value returned. If foo is extrinsic, then you could use:

x = zeros(3,4);
x = foo(y);
to call foo extrinsically and assign the output as a 3-by-4 array. See:

http://www.mathworks.com/help/simulink/ug/calling-matlab-functions.html#bq1h2z9-47 http://www.mathworks.com/matlabcentral/answers/122167-subscripting-into-an-mxarray-is-not-supported

for more details.

In your case you can do an assignment:

% Assign Prob the right, size, type, complexity for Coder
Prob = zeros(m,n,k,...);
Prob = softmax(terms);

That will allow you to treat Prob as any other MATLAB variable again.

Best,

Ryan

"Tan Biru" wrote in message <lu2jr3$2ba$1...@newscl01ah.mathworks.com>...
0 new messages