Value not multiplying by -1
Show older comments
I have a nested if statement which works as it is supposed to until the last iteration.
Does anyone know why it is not multiplying the last value by -1?
w = x_0 % zero vector
k = 0 % zero value
i=1 % initial value for count
j=-1 % coefficient
for i = 1:n
if Raw_Data(i,2) == Raw_Data (i,3)
fprintf ('ERROR!!!!!)
else if Raw_Data(i,3) == val
w(i,1) = Raw_Data(i,4)
else if Raw_Data(i,2) == val
w(i,1) = j* Raw_Data(i,4)
% Rationalisation Not Working
else w(i,1)= j* Raw_Data(i,4)
end
end
end
i=i+1
end
Accepted Answer
More Answers (2)
Ingrid
on 7 Apr 2015
in a for loop there is no need to increment the i so just leave out
i = i+1
as this is done automatically.
Also do not use the i as this can be confusing when using imaginary numbers, better practice is to use ii or something completly else (for example counter )
It is not clear why there is an
else if Raw_Data(i,2) == val
when you perform the same function regardless of true/false of this expression
1 Comment
Jay
on 7 Apr 2015
Ilham Hardy
on 7 Apr 2015
Edited: Ilham Hardy
on 7 Apr 2015
1 vote
Learn on how to debug your code,
3 Comments
Jay
on 7 Apr 2015
Ilham Hardy
on 7 Apr 2015
Ok, did you also notice the mlint warning on your code?
e.g.:
There should not be empty char (read: space) between else and if. So, elseif instead of else if .
Jay
on 7 Apr 2015
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!