# How to solve "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side" error?

5 views (last 30 days)
Neeraj Kumar on 15 Sep 2020
Commented: Neeraj Kumar on 15 Sep 2020
I am not understanding this indices error. Please, take a look at the code and help me with the solution. I have highlighted the line in the code in which the error is shown.
dis0=input('dis0=');
disd0=input('disd0=');
phi0=input('phi0=');
phid0=input('phid0=');
theta0=input('theta0=');
fmin=input('fmin=');
fmax=input('fmax=');
for f=fmin:fmax
tp=1/f;
dt=1/(25*f);
for i=0:dt:2*tp
t=i;
j=i+2;
f1=sin(2*pi*f*t);
f2=a*sin(2*pi*f*t);
f3=b*sin(2*pi*f*t);
F=[f1; f2; f3];
if i==0
% The error is shown in the below line.
x(j)=[dis0; phi0; theta0];
xdd(j)=M\(F-(K*x(j))-(C*xd(j)));
else
break
end
x(j-1)=x(j)-(dt*xd(j))+(0.5*(dt^2)*xdd(j));
x(j+1)=((M/(dt^2))+(C/(2*dt)))\(F-(K-((2*M)/(dt^2)))*x(j)-((M/(dt^2))-(C/(2*dt)))*x(j-1));
end
end

Cris LaPierre on 15 Sep 2020
x(j) is a single array element. You are trying to assign it 3 values, [dis0; phi0; theta0]. I'm unsure what the intent is, but one way to solve this is to specify row and column indices:
x(:,j)=[dis0; phi0; theta0];
You will need to double check everywhere you use x to take this change into account.

Steven Lord on 15 Sep 2020
You have an egg carton and eggs that fit into the carton, one egg per cup in the carton. You're not allowed to break / scramble the eggs. How do you put three eggs into one cup of the egg carton?
That's effectively what you're trying to do with that line of code. In an expression A(ind) = B, A is the carton, ind selects some number of cups in the carton, and B is the collection of eggs you're trying to put in the cups.
Cris LaPierre suggested putting the eggs not in a single cup of the egg carton but in multiple cups, one per egg.
Cris LaPierre on 15 Sep 2020
This code does not create an error:
x(:,j)=[dis0; phi0; theta0];
It is the next line of code that does:
You will need to make a similar change to handle xd and xdd. You will then need to modify everywhere in your code you use x(j) and its variations: x(j-1) and x(j+1), as well as xd(j) and xdd(j).
For xdd, you have a more complicated issue since M\(F-(K*0)-(C*0)) produces a 3x3 array. You might need to do something like
xdd(:,:,j)=M\(F-(K*0)-(C*0));
Neeraj Kumar on 15 Sep 2020
Thank you Cris for the solution and Thank you Steven for the analogy.

R2019a

### Community Treasure Hunt

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

Start Hunting!