Value not multiplying by -1

2 views (last 30 days)
Jay on 7 Apr 2015
Edited: Thorsten on 7 Apr 2015
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
4 CommentsShow 2 older commentsHide 2 older comments
Adam on 7 Apr 2015
So since you used breakpoints in the code where exactly is the problem? Is it not going into the relevant else-if statement or is the code in there not executing correctly?
Jay on 7 Apr 2015
The code is not executing correctly.
Namely the
elseif Raw_Data(i,2) == val
w(i,1)= j* Raw_Data(i,4)
The element is not multiplying by -1.
The value is already -ve so I want to multiply it by -1 again to = +ve value.

Sign in to comment.

Accepted Answer

Thorsten on 7 Apr 2015
Edited: Thorsten on 7 Apr 2015
I would suggest to avoid the loop and use logical indexing:
ind = Raw_data(i,3) == val;
w(ind) = Raw_data(:,4);
w(~ind) = j*Raw_data(:,4);
if any(Raw_Data(i,2) == Raw_Data (i,3))
error('values in column 2 and 3 are identical.')
end
Note that in your code
else if Raw_Data(i,2) == val
is not needed, because
w(i,1) = j* Raw_Data(i,4)
in any case.
And if w is a vector, w(i) is the same as w(1,i).
0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

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 CommentShow -1 older commentsHide -1 older comments
Jay on 7 Apr 2015
I will leave out the incremental addition.
I will also change the variable from i to something more suitable.
I don't see how
else if Raw_Data(i,2) == val
is performed regardless, val is a dynamic variable.
else w(i,1)= j* Raw_Data(i,4)
was used to try and rectify the issue of the final element not multiplying by -1.

Sign in to comment.

Ilham Hardy on 7 Apr 2015
Edited: Ilham Hardy on 7 Apr 2015
3 CommentsShow 1 older commentHide 1 older comment
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
Ok, I will do that from now on.
It does not throw an error if I use else if. Is there a difference in the function elseif and else if?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!