Using loops to perform vector functions
Show older comments


I'm new to MATLAB and I'm having trouble with my function. I'm trying to call upon a different operation "op" to do subtraction instead of addition but despite entering op as 2 it's still doing addition. Any help is greatly appreciated.
3 Comments
Torsten
on 26 Jan 2022
Maybe a2 = 0 ?
Jonathan Lam
on 26 Jan 2022
Torsten
on 26 Jan 2022
You set op=1 within the function.
Accepted Answer
More Answers (1)
KSSV
on 26 Jan 2022
0 votes
In the function you have given input op which decides wheter to add ot subtract. But you are over writitng the input op inside the code to op = 1. So always the function adds. You need to remove the line op = 1 i.e. ine number 10 in the code.
Also your variable result has to be intiailized before the loop.
1 Comment
Jonathan Lam
on 26 Jan 2022
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!