Clear Filters
Clear Filters

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

12 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
Thanks a lot dear Walter Roberson for your efforts. I ran it. Now its working. I get the graph. But when I removed the semicolons from "xmin" and "fmin" to see their values.In this xmin is the estimated vector which must nearly resemble my u vectro in the "obj.m" file. But I see that its not the same as my u vector. A lot of variations in the values and sometimes their values changes positions as well. As you can see my different runs:
>> myfit_driver
xmin =
0.9932 2.1081 2.9842 3.1416
fmin =
0.7489
>> myfit_driver
xmin =
0.9396 1.9000 3.0854 3.1416
fmin =
0.7578
>> myfit_driver
xmin =
0.9186 1.9747 2.9763 3.1416
fmin =
0.7447
>> myfit_driver
xmin =
1.0536 2.0371 3.1416 3.1416
fmin =
0.7612
>> myfit_driver
xmin =
1.0377 2.0226 2.8995 3.1416
fmin =
0.7489
>> myfit_driver
xmin =
0.9598 1.9679 2.8777 3.1416
fmin =
0.7545
>> myfit_driver
xmin =
1.0721 1.8975 3.0554 3.1416
fmin =
0.7556
>> myfit_driver
xmin =
1.0148 1.8979 2.9469 3.1416
fmin =
0.7503
>> myfit_driver
xmin =
0.9659 2.0040 3.1416 3.1416
fmin =
0.7581
>> myfit_driver
xmin =
0.9647 2.0892 3.0875 3.1416
fmin =
0.7537
>> myfit_driver
xmin =
0.8858 1.9868 3.0025 3.1416
fmin =
0.7501
>> obj_driver
xmin =
1.7087 5.7792 1.4902 0.8463
fmin =
1.9548
>> obj_driver
xmin =
3.0668 5.6929 0.8875 1.4876
fmin =
3.0838
>> obj_driver
xmin =
1.4781 5.9054 1.5567 0.8601
fmin =
1.7798
>> obj_driver
xmin =
2.4717 8.4429 0.8501 1.4941
fmin =
3.8352
>> th=pi/180;
u=[2 7 50*th 85*th]
u =
2.0000 7.0000 0.8727 1.4835
>> obj_driver
xmin =
1.4817 7.0154 0.9122 1.5292
fmin =
0.7388
>> obj_driver
xmin =
6.6405 2.0153 0.8459 1.4795
fmin =
0.5694
>>
Now what to do dear?
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!