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

3 views (last 30 days)
malak Osama on 22 Jul 2021
Answered: KSSV on 23 Jul 2021
I am unable to solve the error I am facing. It's with the ax,ay, az and I think the problem is either in the way I am implementing them or in the stuff in their equations, the xs,ys,zs or the X,Y,Z. Please if someone can have a look at the code below I commented which line the error is. Maybe someone can advise me how to fix it. Thanks.
clear all;
clc;
c=299792458;
newdata=[];
shj=1;
i=1;
N=1;
SecOfDay=30;
counter_1=0; %counts how many rows belongs to the same seocnds in the data
for S=1:length(data3)
if(data3(S,6)>15)
newdata(i,:)=data3(S,:);
i=i+1;
end
end
while N<length(newdata)
for r=1:length(newdata)
if(SecOfDay==newdata(r,1))
counter_1=counter_1+1;
end
xs=newdata(N:counter_1,3);
ys=newdata(N:counter_1,4);
zs=newdata(N:counter_1,5);
dist=newdata(N:counter_1,6);
end
Fun = @(u) [sqrt((xs-u(1)).^2 + (ys-u(2)).^2 + (zs-u(3)).^2) - (c*u(4)) - (dist)];
x0 = [0;0;0;0];
u = lsqnonlin(Fun,x0);
SecOfDay=SecOfDay+30;
X=u(1);
Y=u(2);
Z=u(3);
ERROR=u(4);
r= sqrt((xs-X).^2-(ys-Y).^2-(zs-Z).^2);
ax(N:counter_1)=(xs-X)/r; %the error is at this line
ay(N:counter_1)=(ys-Y)/r;
az(N:counter_1)=(zs-Z)/r;
N=counter_1+1;
H=[ax ay az 1];
for i=N:counter_1
HH=[H; ax(i) ay(i) az(i) 1];
end
end
malak Osama on 23 Jul 2021
No issuies. thank you again, really appreciate it.

KSSV on 23 Jul 2021
The error is clear, this error pops out when you are trying to save more number of elements than the initialized array.
Example:
A = zeros(5,5) ; % A is 5*5 matrix initialized
A(1,:) = rand(1,5) ; % no error as 5 elements are saved
A(2,:) = rand(1,7) ; % error becuase in second row of A should have 5 elemnts
Check the matrix you have intiliazed. If you are not aware of of the dimensions, store them in a cell array.
A = cell(5,1) ;
A{1} = rand(1,5) ;
A{2} = rand(1,6) ;

R2020b

### Community Treasure Hunt

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

Start Hunting!