Error: Unable to perform assignment because the left and right sides have a different number of elements.

2 views (last 30 days)
Using it as a part of Genetic Algorithm optimization. The code was working fine in its original form. But when tried to optimize by using my objective function, I got into this error
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Mutate (line 25)
y(j)=x(j)+sigma*randn(size(j));
Error in ga (line 144)
popm(k).Position=Mutate(p.Position,mu,VarMin,VarMax);
function y=Mutate(x,mu,VarMin,VarMax)
nVar=numel(x);
nmu=ceil(mu*nVar);
j=randsample(nVar,nmu);
sigma=0.1*(VarMax-VarMin);
y=x;
y(j)=x(j)+sigma*randn(size(j));
y=max(y,VarMin);
y=min(y,VarMax);
end

Answers (2)

KSSV
KSSV on 26 Oct 2020
This line:
y(j)=x(j)+sigma*randn(size(j));
will create error..because, you are trying to save more number of elements then it is initialized. Try:
y(:,:,j)=x(j)+sigma*randn(size(j));
or
y{j}=x(j)+sigma*randn(size(j));
You have to check the dimensions/ size of the RHS and intialize the LHS to store values.
  6 Comments
Muhammad Irfan
Muhammad Irfan on 26 Oct 2020
function y=Mutate(x,mu,VarMin,VarMax)
nVar=numel(x);
nmu=ceil(mu*nVar);
j=randsample(nVar,nmu);
sigma=0.1*(VarMax-VarMin);
y=zeros(size(x));
y=x;
y(j)=x(j)+sigma.*randn(size(j));
y=max(y,VarMin);
y=min(y,VarMax);
end
Got this error after this:
Matrix dimensions must agree.
Error in Mutate (line 25)
y(j)=x(j)+sigma.*randn(size(j));
Error in ga (line 148)
popm(k).Position=Mutate(p.Position,mu,VarMin,VarMax);
Walter Roberson
Walter Roberson on 26 Oct 2020
You have not shown your call to ga. As outside observers, we do not know that the VarMin and VarMax that you show the initialization for, are the same ones that are passed to Mutate .
Again I recommend dbstop if error and examine the variables when the code stops.

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Oct 2020
Okay, so your problem is that your x is not the orientation you expect.
When you index a vector of values at a vector of indices, then the shape of the result depends upon the shape of the original vector, not the shape of the index.
>> A = [3 7 9]
A =
3 7 9
>> A([1 3])
ans =
3 9
>> A([1 3].')
ans =
3 9
>> B = A.'
B =
3
7
9
>> B([1 3])
ans =
3
9
>> B([1 3].')
ans =
3
9
Meanwhile, you forced a specific orientation onto your j and so your randn(size(j)) has a specific orientation.
Suppose for example that your x is a 9 x 1 column vector and your randn(size(j)) is a 1 x 9 row vector then when you index x(j) you get back a column vector, and since R2016b, column vector + row vector gives you back a full-sized vector instead of giving you an error message.
  5 Comments
Walter Roberson
Walter Roberson on 14 Apr 2023
j is 9 x 1. randn(size(j)) would be 9 x 1. sigma(j) would be 1 x 9 because sigma is a row vector. So you have a 1 x 9 .* a 9 x 1, which is going to give you a 9 x 9 result.
Reminder that when you index a vector at a vector, the shape depends on the shape of the vector being indexed, not upon the shape of the indexing array.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!