# Please help me to understand Error "The expression to the left of the equals sign is not a valid target for an assignment.

3 views (last 30 days)
Khairul Nur on 13 Nov 2019
Commented: Khairul Nur on 13 Nov 2019
hi, i have this code a error "The expression to the left of the equals sign is not a valid target for an assignment." Please help me to understand, since the function can be execute outside the while loop. However, its return the error when run inside the while loop.
objec_func = 0;
%no of cluster
k = 4;
%set initial centroid
c1 = [13 25 15 11 11 14 12 12 10 14];
c2 = [13 25 14 11 11 14 12 13 13 13];
c3 = [13 24 14 10 11 14 12 12 12 15];
c4 = [13 25 14 11 11 13 12 12 12 13];
%calculate distance
distance=d_euclidean(data,c1,c2,c3,c4);
%partition_matrix
pm = p_matrix(distance)
%objective function
objective = obj_function(distance,pm)
%calculate mean
[cc1,cc2,cc3,cc4] = calc_centroid(data)
%calculate distance
distance2=d_euclidean(data,cc1,cc2,cc3,cc4);
%partition_matrix
pm2 = p_matrix(distance2)
%objective function
objective2 = obj_function(distance2,pm2)
while(objective2 < objective)
{
[cc1,cc2,cc3,cc4] = calc_centroid(data) %the error come from this line
distance2=d_euclidean(data,cc1,cc2,cc3,cc4);
pm2 = p_matrix(distance2)
objective2 = obj_function(distance2,pm2)
}
##### 1 CommentShowHide None
KALYAN ACHARJYA on 13 Nov 2019
objec_func=0;
%no of cluster
k=4;
%set initial centroid
c1=[13 25 15 11 11 14 12 12 10 14];
c2=[13 25 14 11 11 14 12 13 13 13];
c3=[13 24 14 10 11 14 12 12 12 15];
c4=[13 25 14 11 11 13 12 12 12 13];
%calculate distance
distance=d_euclidean(data,c1,c2,c3,c4);
%partition_matrix
pm=p_matrix(distance)
%objective function
objective=obj_function(distance,pm)
%calculate mean
[cc1,cc2,cc3,cc4]=calc_centroid(data)
%calculate distance
distance2=d_euclidean(data,cc1,cc2,cc3,cc4);
%partition_matrix
pm2=p_matrix(distance2);
%objective function
objective2 = obj_function(distance2,pm2);
while objective2<objective
[cc1,cc2,cc3,cc4]=calc_centroid(data) %the error come from this line
distance2=d_euclidean(data,cc1,cc2,cc3,cc4);
pm2=p_matrix(distance2);
objective2=obj_function(distance2,pm2)
end

Stephen23 on 13 Nov 2019
Edited: Stephen23 on 13 Nov 2019
The problem is your invented syntax using { and }, which looks like you are trying to write code using a different programming language. In any case, you can avoid the error simply by getting rid of those pointless { and } characters, and by adding the required end at the end of the while loop.
This is also a good example of why it is important to pay attenton to the warnings shown by the MATLAB editor, which indicates in two locations that your invented syntax has some problems: It is also a good example of how using the default code alignment helps to detect basic code bugs: if you had aligned the code using the MATLAB editor's default setting, then the code inside the while loop is clearly no longer aligned and indicates a syntax error.
It is also a good example of why it is important to read the documentation for every operator and function that you use (no matter how trivial you might think it is). Do you see any examples on the while help that look anything like the code that you wrote? (hint: no).
In any case, when I run my test code I get: When I remove the pointless { and } characters and add end, the code works without error:
while(1 < 2)
[R,C] = size(3)
end
although of course it does not stop...
##### 2 CommentsShowHide 1 older comment
Khairul Nur on 13 Nov 2019
thank you very much for the explainantion . yes, i new in mathlab language..huhu

### More Answers (1)

KSSV on 13 Nov 2019
Edited: KSSV on 13 Nov 2019
A(1:4) = rand(1,5) ;
The above will throw error. Becuase A is intialized as 1X4, we are trying to replace it with 1X5.
Stephen23 on 13 Nov 2019
@KSSV: Can you show any LHS indexing in the code given in the question?