MATLAB Answers

Why unifrnd(lb,ub,[nPop,D]) gives error?

110 views (last 30 days)
Sadiq Akbar
Sadiq Akbar on 26 Dec 2020
Commented: Rik on 3 Jan 2021
This question was flagged by Walter Roberson
Why the following code gives error?
lb=[0 0 0 0]; ub=[10 10 pi pi]; nPop=30; D=4;
unifrnd(lb,ub,[nPop,D])
Error using unifrnd
Size information is inconsistent.
  1 Comment
Rik
Rik on 3 Jan 2021
Deleted comments can be found in the Google cache (the archived version will require modification of the attributes, as the show/hide comments toggle doesn't execute properly).

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 26 Dec 2020
This cannot be done in single call to unifrnd. You need a for-loop
lb = [0 0 0 0]; ub=[10 10 pi pi]; nPop=30;
D = numel(lb);
M = rand(nPop, D);
for i = 1:D
M(:,i) = unifrnd(lb(i),ub(i),[nPop,1]);
end
  16 Comments
Walter Roberson
Walter Roberson on 28 Dec 2020
Code seems to be working for me.
Invoke obj_driver or myfit_driver as applicable.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!