Choice-Dependent Multi-Variable Anonymous Function

A little background information. There are two parking lots, one which is long term and one which is short term. For the short term lot, the first 30 minutes are free, and each additional 20 minutes or fraction thereof is $1. The daily maximum is $25. For the long term lot, the first hour is $1 and each additional hour or fraction thereof is $1. The daily maximum is $6 and the weekly maximum is $42.
I have to create an anonymous function y=cost(L,W,D,H,M). L can be either 'long' or 'short', while the other values are numerical. W is the number of weeks, D is the number of extra days, H is the number of extra hours, and M is the number of extra minutes. I cannot for the life of me figure out how to properly do this without using for-loops and if statements, and even then it isn't working properly. Can anyone tell me how I can implement the string value of L into the function alongside numerical values?
Any help would be greatly appreciated.
Thanks.

 Accepted Answer

strcmp(L,'long') * min(42, ...) + strcmp(L,'short') * min(25, ...)

2 Comments

Considering that we haven't learned about strcmp in our class, nor has it been covered in my textbook anywhere, I'm not sure our professor would want us to use it. Unless of course there is no other way.
For the min function, would the ... represent other numbers or a variable?
The ... represent formula.
You can use isequal() instead of strcmp.
If you are forced to you could use
(length(L)==4 && L=='short')
but be careful because here it would be crucial that && was used as the == test should not be used with strings unless you are absolutely sure the string is the same size as what you are comparing it to.
What I showed above was a generalized mechanism by which you can select one of two different formula. You calculate the price both ways, but you multiply one of the formula by 0 if that formula tests out to be not applicable (wrong kind of parking).
Suppose, by the way, that someone stays 3 weeks and some-odd hours in long term parking: would that imply 3 times the weekly maximum plus the price for the left-over hours? And if they stayed the same length of time in the short-term parking, it would be 21 (3 weeks) times the daily maximum plus the price for the odd hours?
Does the "first 30 minutes are free" rule apply to people who have stayed more than one day? e.g., if I leave after 1 day and 20 minutes, is that just the $25 daily fee because the 20 minutes of the second day were free, or would it be $25 + $1 because 20 minutes is "additional 20 minutes or fraction thereof" with the free first 30 minute parking only applying on the first day?

Sign in to comment.

More Answers (0)

Categories

Find more on Language Fundamentals 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!