This question is closed. Reopen it to edit or answer.
How to struct ?
1 view (last 30 days)
HI, can anyone tell me is this correct?
Tagprice = 1:6000; if Tagprice < 200 D = 0; C = 0.25; E = 0.14; Price_D(Tagprice)=Tagprice*(1-D); Store.Item1.Tag_price=Tagprice ; Store.Item1.Price_after_discount=Tagprice*(1-D); Price_C(Tagprice)=Tagprice*(1-C); Store.Item2.Tag_price=Tagprice ; Store.Item2.Price_after_discount=Tagprice*(1-C); Price_E(Tagprice)=Tagprice*(1-E); Store.Item3.Tag_price=Tagprice ; Store.Item3.Price_after_discount=Tagprice*(1-E); end
Eric on 19 Feb 2018
Edited: Eric on 19 Feb 2018
It's not quite right, but you're on the right track. First, you should have TagPrice = 1:200:7000, according to the question. Second, the conditional at the top is not doing what you probably intended it to do... remove it. Once you do these things, you can use conditional indexing to set the discount for each of the different price ranges for each item. Right now you are using the same discount (namely the first) for each item, regardless of price. For example, the most straightforward approach for Item 1 would look something more like:
Store.Item1.Tag_price = TagPrice;
DiscountPrice = TagPrice;
DiscountPrice(200<=TagPrice & TagPrice<600) = (1-0.015).*TagPrice(200<=TagPrice & TagPrice<600);
DiscountPrice(600<=TagPrice & TagPrice<1200) = (1-0.03).*TagPrice(600<=TagPrice & TagPrice<1200);
Store.Item1.Price_after_discount = DiscountPrice;
The vertical ellipsis would be filled in according to each price range in the table, and you would repeat this whole thing for all 3 items. Obviously, this is not the best/neatest/most adaptable/most efficient approach and it can be by using things like for loops, a vector of price boundaries, a matrix of discounts, indexing your structure (e.g. Store.Item(2)), etc. Since this looks like a homework question, I'll leave it to you to figure out how to improve your code with these suggestions, although you can always ask another question if you get stuck.