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 commentaires

Rik
Rik le 3 Mai 2022
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.
Jan Horacek
Jan Horacek le 3 Mai 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.

Connectez-vous pour commenter.

Réponses (1)

Stephen23
Stephen23 le 3 Mai 2022
Modifié(e) : Stephen23 le 3 Mai 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

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2021b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by