简体   繁体   中英

How to convert Matlab code to Delphi?

How to convert this part of Matlab code to Delphi?

for i=1:popsize 
    fi=rand(1,dimension); % Generate a vector of uniform random numbers
    p=pbest(i,:);
    pbest(i,:)=x(i,:);
end

My code:

for i:= 1 to popsize do
begin
  fi:= // which function generates vector of uniform random numbers in Delphi?
  for k :=1 to popsize do
  begin
    p:=pbest(i,k);
    pbest(i,k):=x(i,k);
  end;
end;

You can call Random function to generate a uniformly distributed random value. Calling Randomize once makes Random generate different values in each run.

var
  fi: array of Double;
  J: Integer;
begin
  Randomize;
  for J := 0 to dimension - 1 do
    fi[J] := Random;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM