MATLAB Answers


replace the function "randperm" into code from embedded block SIMULINK

Asked by Emmanuel Luevano on 19 Nov 2012

hi, I need to generate a c code from the next code wrote on an embedded editor from embedded block from simulink, but randperm is not supported by code generation, is there some way to do it?

thanks for you support

Size = 100

n = 50

pop = zeros(Size,n);
pop(1,:) = (1:n);
for k = 2:popSize
  pop(k,:) = randperm(n);

  1 Comment

btw, if I use eml.extrinsic to execute it, I understand that the operation is performed externally, but, it affect my code when this is done?

1 Answer

Answer by Jan Simon
on 20 Nov 2012
 Accepted answer

randperm() is actually:

[~, p] = sort(rand(1,n))

Does this work in code generation?

In the current Matlab version, randperm uses the smarter Fisher-Yates-Shuffle, when 2 inputs are used. Would a corresponding C-Mex function work: FEX: Shuffle ?


these are good tips, I'll try with these, thanx, back with the results.

Jan Simon
on 21 Nov 2012

Another useful implementation of the Fisher-Yates-Shuffle in pure Matlab:

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!