Different "kind" of variable

1 view (last 30 days)
Jan Horacek
Jan Horacek on 3 May 2022
Commented: Jan Horacek on 3 May 2022
Hi, I would like to ask you how could I create different "kinds" of variable. I am going to create school project about food-macros tracking in Matlab. I need to create three "kinds" of variable - carbohydrates, fats and proteins and I need them to sum by their "kind". How can I do this ?
Thanks, all answers are welcomed !
  2 Comments
Jan Horacek
Jan Horacek on 3 May 2022
In that program there will be list of food with their macros per 100g/100ml, for example oats (66g carbs, 10g proteins, 7g fats) and milk (12g carbs, 8g proteins, 8g fats) and I need to sum all these macros separately. For example: 50g of oats and 200g of milk means 66/2 + 2*12 CARBS, 10/2 + 2*8 PROTEINS and 7/2 + 2*8 FATS. I hope you understand.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 3 May 2022
Edited: Stephen23 on 3 May 2022
typ = ["oats","milk","water"]
typ = 1×3 string array
"oats" "milk" "water"
mat = [66,12,0;10,8,0;7,8,0]
mat = 3×3
66 12 0 10 8 0 7 8 0
inp = ["milk","oats"];
qty = [2,0.5];
[~,idx] = ismember(inp,typ);
out = qty*mat(idx,:)
out = 1×3
53 22 0

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!