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

11 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
  31 Comments
Sadiq Akbar
Sadiq Akbar on 28 Dec 2020
Further, when i rant it with my own old fitness function (before your chnages), again it gave error as:
Subscripted assignment dimension mismatch.
Error in fun2sn0 (line 13)
[~, ix1(ix)] = sort(b); % temp stores the randomly generated vector "best" by algorithm
Error in GQPSO (line 54)
f_x(i) = feval(fun,x(i, :));
Error in myfit_driver (line 2)
[xmin,fmin,histout] = GQPSO(@fun2sn0,4,30,[0 0 0 0],[10 10 pi pi],2000,10000*4);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!