Different "kind" of variable

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

If you come up with a specific example of how your data should look and what output you expect it will be easier to give you advice.
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

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Release

R2021b

Tags

Asked:

on 3 May 2022

Commented:

on 3 May 2022

Community Treasure Hunt

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

Start Hunting!