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

2 Ansichten (letzte 30 Tage)
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 Kommentar
KALYAN ACHARJYA
KALYAN ACHARJYA am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 13 Nov. 2019
Bearbeitet: Stephen23 am 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 Kommentare
John D'Errico
John D'Errico am 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 am 13 Nov. 2019
thank you very much for the explainantion . yes, i new in mathlab language..huhu

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 13 Nov. 2019
Bearbeitet: KSSV am 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 Kommentare

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Package MATLAB Functions finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by