Error: File: square_matrix.m Line: 12 Column: 16

조회 수: 1 (최근 30일)
Bennie
Bennie 2012년 2월 27일
Hello I am trying to apply the rule:(S(i,j))^t = (S(i,j-1))^t+ (S(i-1,j))^t+ (S(i,j))^t + (S(i+1,j))^t + (S(i,j+1))^t
I am getting an error for entering the matrix incorrectly. Below is my full code. clear; clc; clf; disp('Starting...');
S=zeros(50);
newS=zeros(50);
%i=0:5;
%j=0:5;
for t=1:200
for i= 1:5
j= 1:5
(S(i,j))^t = (S(i,j-1))^t+ (S(i-1,j))^t+ (S(i,j))^t + (S(i+1,j))^t + (S(i,j+1))^t;
if 0<=S(i,j)< 3
then S(i,j)=0
elseif 3<= S(i,j)<= 5
then S(i,j)=1
end
end
newS=S(i,j)
end
spy(newS)
actual error when run: ??? Error: File: square_matrix.m Line: 12 Column: 16 The expression to the left of the equals sign is not a valid target for an assignment.
Please advise.

답변 (1개)

Jan
Jan 2012년 2월 27일
What do you expect "^t" to do? It means "power to t". If you want to transpose a matrix, swap the indices instead.
The error message means, that you cannot apply a function on the left hand side of an assignment.
After you have fixed this, the next problems will appear:
if 0<=S(i,j)< 3
In Matlab this calculates:
S(i,j) < 3 ==> TRUE or FALSE
0 <= TRUE or FALSE
which is TRUE always, because TRUE is evaluated as 1 and FALSE as 0. You want:
if 0 <= S(i,j) && S(i,j) < 3
In addtion there is no then keyword in Matlab.
What are you expecting as result of these lines:
for i= 1:5
j= 1:5
?
The line:
newS=S(i,j)
overwrites the matrix newS in the first iteration and replaces the scalar value in all subsequent iterations. Therefore newS is a scalar at the end. Do you want newS(i, j)?
Finally I think the shown code is far away from a valid Matlab syntax. I suggest to read the Getting Started chapters of the documentation to learn the basics of this powerful programming language.

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by