why are if statements not working?

%the if statements are not working when I run the code
Price=[199.54 195.89 195 192 193.29 197.09 197.78 190.34 189.55 193.30...
196 194.60 193.63 193 193.77 195.23 193.97];
Total_Shares=100;
Cash=0;
Fee=10;
Shares=10;
if Price(1:end)>195
a=Total_Shares-Shares
Value=a*Price(1:end)
elseif Price(1:end)<193.50
a=Total_Shares+Shares
Value=a*Price(1:end)
end
if Price(1:end)>195
c=Shares*Price(1:end)
Total_cash=c-Fee
elseif Price(1:end)<193.50
c=Shares*Price(1:end)
Total_cash=c-Fee
end

Answers (2)

Cris LaPierre
Cris LaPierre on 9 Dec 2020
For an if statement to work as exepcted, the conditional statement must return a single logical result. Yours is returning a result for each value in Price. If you want to perform a computation for each value in Price you should consider placing everything inside a for loop.
You could instead consider removing the if statements and performing the computations using logical indexing.
Consider looking at Chs 12-13 in MATLAB Onramp.

5 Comments

Although people who have worked with MATLAB long enough now know to "expect" what actually happens with non-scalar tests, it is almost never a good idea to rely upon the built-in behaviour, and to instead call all() manually if you want the behaviour that MATLAB implements.
It is not that I would imagine that Mathworks will change what the built-in behaviour does : the point is that it is much easier for people to read and understand the code when all() is used specifically, and that withotu the all(), programmers who know what MATLAB actually does would be forced to go carefully through the code to make sure that the built-in behaviour is the correct behaviour for context. The amount of time lost and confusion generated by deliberately not using all() on non-scalar tests far outweighs any microsecond advantage there might be to not calling all() yourself.
I guess my interpretation was that the intent was to take any values >195 and process them through one set of equations, and then those that were <193.5 would be calculated through the 2nd set of equations. This is where logical indexing could be helpful.
If the intent was instead for the test to be all or nothing, then returning a single logical result is best achieved using all.
The OP has not provided enough detail to know what is actually intended.
In my experience, nearly everyone intends if statements that operate on non-scalar objects to somehow process the chain of if on an element-by-element basis, outputing to corresponding locations without thinking about indexing...
I have, though, encountered a small number of "professional" programs that take advantage of the all() behaviour, without documentation. The most recent one I dealt with (last year) stored a mix of numeric 0 and graphics handles in the same vector and relied on the implicit all() to determine whether the graphics handles had all been assigned yet. Written back in the day when graphics handles were numeric. Other parts of the same code put graphics handle and x and y coordinates into the same vector, again taking advantage of the fact that graphics handles were numeric at the time the code was written.
The code was being too clever for its own good, and it broke when graphics handles became objects. You can still assign graphics handles into doubles, but there were problems anyhow. And the code vector testing the mix of 0 and graphics handles was not even correct code, as there were ways that the graphics objects could get deleted, and the code was only testing if the handle had been recorded at some time, not whether it was still valid...
It really is about time that this ancient "feature" was retired, it causes far more problems than it has ever solved. Ditto for the "feature" of for-looping over columns.
From my own observation over thousands of lines of code and functions that I have used, tested, and reviewed, I have never seen one single effective use of either of those "features", but I have certainly read on this forum about plenty of bugs caused by them.
I have used looping over columns a couple of times. I forget why at the moment, but I do seem to recall that cell arrays were involved.

Sign in to comment.

Price=[199.54 195.89 195 192 193.29 197.09 197.78 190.34 189.55 193.30...
196 194.60 193.63 193 193.77 195.23 193.97];
Total_Shares=100*ones(size(Price));
Cash=0;
Fee=10;
Shares=10;
Total_cash=0;
Value=100*Price;
Total_Shares(Price>195)=Total_Shares(Price>195)-Shares;
Value(Price>195)=Total_Shares(Price>195).*Price(Price>195);
Total_Shares(Price<193.5)=Total_Shares(Price<193.5)+Shares;
Value(Price<193.5)=Total_Shares(Price<193.5).*Price(Price<193.5);
Total_cash=Total_cash+sum(Shares*Price(Price>195)-Fee)-sum(Shares*Price(Price<193.5)+Fee);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 9 Dec 2020

Commented:

on 9 Dec 2020

Community Treasure Hunt

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

Start Hunting!