MATLAB Answers

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

조회 수: 1(최근 30일)
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.
data = readtable('DATA3.txt');
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

KALYAN ACHARJYA
KALYAN ACHARJYA 13 Nov 2019
data=readtable('DATA3.txt');
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

로그인 to comment.

채택된 답변

Stephen Cobeldick
Stephen Cobeldick 13 Nov 2019
Stephen Cobeldick 님이 편집함. 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

John D'Errico
John D'Errico 13 Nov 2019
Stephen is exactly correct here.
The error message is confusing, in the sense that MATLAB is itself completely confused. You have used invalid syntax in THIS language. But as it turns out, the presense of the pair {} allows the parser to not throw an immediate error, before then trying to execute the code. In fact, it tries to interpret what was given to it as something valid, although the editor does see something in the code as something strange, and flags the strangeness.
In general, ALWAYS look at those lines flagged by the editor. They have a red or orange line on the right side of the editor window. Hover your mouse on top of the red line, and read the message it reports. Resolve each of those problemic statements flagged, before trying to run your code..
The root of the problem is as Stephen says, that you are still apparently thinking in a different language, writing code for some unnamed language, which must use the { and } to delimit a loop. That explains why you failed to use an end statement, as required by MATLAB.
Khairul Nur
Khairul Nur 13 Nov 2019
thank you very much for the explainantion . yes, i new in mathlab language..huhu

로그인 to comment.

More Answers (1)

KSSV
KSSV 13 Nov 2019
KSSV 님이 편집함. 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.

  댓글 수: 3

Stephen Cobeldick
Stephen Cobeldick 13 Nov 2019
@KSSV: Why do you think that would throw an error?:
>> A = zeros(1,4) ;
>> A = rand(1,5)
A =
0.09754 0.2785 0.54688 0.95751 0.96489

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

태그


Translated by