如何将字符串值添加到不同大小的数组中?
2次观看(最近30天)
显示较旧的评论
嘿,我是一个为个人项目编码的开头。我现在的问题是,我正在尝试将字符串值分配到可以更改大小的数组中的位置。我将此“成分列表”数组设置为一个匿名函数,该功能根据成分的数量更改大小。接下来,有if的语句找到了哪顿饭(在先前的功能中随机确定),我需要添加成分,然后将特定成分添加到ingredient_list_lunches/dinners中。我一直遇到一个错误,上面写着:“无法执行分配,因为类型'字符串'的值不可转换为'function_handle'。”
Here's the string array:
incredients_list_tot = [(("Steak"),,,,((“西兰花”),,,,((“萝卜”),,,,(("Mushroom"),,,,(("Rice")...
((“红烧酱”),,,,((“三文鱼”),,,,(("Bok Choy"),,,,(("Potato"),,,,((“黄油”),,,,(("Heavy Cream")...
((“花生酱”),,,,(("Fruit"),,,,((“白洋葱”),,,,(("Tomato"),,,,(("Lettuce"),,,,(("Carne Asada Seasoning"),,,,...
(("Pork"),,,,(("Chicken Broth"),,,,((“培根”),,,,((“卷心菜”),,,,((“鸡”),,,,...
(("Tikka Masala"),,,,((“馕”),,,,(("Lentils"),,,,(("Penn Pot Roast"),,,,((“香肠”),,,,...
(("Shredded Cheese"),,,,(("Penne/Rotini"),,,,(("Garlic"),,,,((“切成丁的西红柿”),,,,(("Pasta Sauce"),,,,...
((“黑豆”),,,,(("Carne Asada Seasoning"),,,,(("Meatballs"),,,,((“奶酪片”),,,,(("Bread"),,,,...
((“番茄罐头”),,,,(("Canned Soup"),,,,((“ Mac和奶酪”),,,,((“热狗”),,,,((“冷冻薯条”),,,,...
((“鱼棒”),,,,(("Chowder"),,,,((“云吞”),,,,(("Flatbread")];
这是我的匿名功能:
ingredient_list_lunches = @(ing_tot_lunches)zeros([1 ing_tot_lunches]);
ing_tot_lunches = 0;
ingredient_list_dinners = @(ing_tot_dinners) zeros([1 ing_tot_dinners]);
ing_tot_dinners = 0;
And here's the code I'm using to add those strings to one list:
为了idx = 1:numel(lunches)
if午餐(1,idx)== prep_meals_list(1,1)%TK Steak
ing_tot_lunches = ing_tot_lunches + 6;
ingredient_list_lunches(idx)=(ingredients_list_tot(1,1));
ingredient_list_lunches(idx)=(ingredients_list_tot(1,2));
ingredient_list_lunches(idx) = (ingredients_list_tot(1,3));
ingredient_list_lunches(idx) = (ingredients_list_tot(1,4));
ingredient_list_lunches(idx)=(ingredients_list_tot(1,5));
ingredient_list_lunches(idx) = (ingredients_list_tot(1,6));
Elseif午餐(1,idx)== prep_meals_list(1,2)%TK鲑鱼
ing_tot_lunches = ing_tot_lunches + 7;
ingredient_list_lunches(idx)=(ingredients_list_tot(1,7));
ingredient_list_lunches(idx) = (ingredients_list_tot(1,8));
ingredient_list_lunches(idx) = (ingredients_list_tot(1,4));
ingredient_list_lunches(idx) = (ingredients_list_tot(1,3));
ingredient_list_lunches(idx)=(ingredients_list_tot(1,2));
ingredient_list_lunches(idx)=(ingredients_list_tot(1,5));
ingredient_list_lunches(idx) = (ingredients_list_tot(1,6));
0评论
答案((2)
克里斯·拉皮埃尔(Cris Lapierre)
2021年8月20日
Edited:克里斯·拉皮埃尔(Cris Lapierre)
2021年8月20日
I'm not sure I understand what you are trying to do with your anonymous functions.
此错误是因为您定义了匿名函数
ingredient_list_lunches
。但是,您将其视为一个变量,试图为其分配一个值。就像说
sin(5)= x
when what you intend is
x = sin(5)
。
As a function handle, you can use
ingredient_list_lunches
就像您将要使用输入并捕获输出的任何其他功能一样。
f = @(x) sin(x);
% works
y = f(pi/2)
%不起作用
f(1) =“ 5”
0评论
史蒂文·洛德(Steven Lord)
2021年8月20日
您的许多变量是不确定的,但我会考虑两种方法之一。这种方法是食谱是索引列表中的曲线量的单元格数组,它很容易定义,但很容易在成分列表中间添加新成分。
incredients_list_tot = [(("Steak"),,,,((“西兰花”),,,,((“萝卜”),,,,(("Mushroom"),,,,(("Rice")...
((“红烧酱”),,,,((“三文鱼”),,,,(("Bok Choy"),,,,(("Potato"),,,,((“黄油”),,,,(("Heavy Cream")...
((“花生酱”),,,,(("Fruit"),,,,((“白洋葱”),,,,(("Tomato"),,,,(("Lettuce"),,,,(("Carne Asada Seasoning"),,,,...
(("Pork"),,,,(("Chicken Broth"),,,,((“培根”),,,,((“卷心菜”),,,,((“鸡”),,,,...
(("Tikka Masala"),,,,((“馕”),,,,(("Lentils"),,,,(("Penn Pot Roast"),,,,((“香肠”),,,,...
(("Shredded Cheese"),,,,(("Penne/Rotini"),,,,(("Garlic"),,,,((“切成丁的西红柿”),,,,(("Pasta Sauce"),,,,...
((“黑豆”),,,,(("Meatballs"),,,,((“奶酪片”),,,,(("Bread"),,,,...
((“番茄罐头”),,,,(("Canned Soup"),,,,((“ Mac和奶酪”),,,,((“热狗”),,,,((“冷冻薯条”),,,,...
((“鱼棒”),,,,(("Chowder"),,,,((“云吞”),,,,(("Flatbread")];
%我trimmed the duplicate ("Carne Asada Seasoning")
食谱={[1 2 6],...百分比与西兰花
[15 16 20 35 36],...% BLT with cheese
}
shoppingList = [ingredients_list_tot(recipe{1}), ingredients_list_tot{recipe{2}}].'
或者,您可能想建立一个
containser.map
or a
table
。This has the benefit of being able to specify dishes and/or ingredients in various orders and to use those names to update the recipes at the expense of taking up more room (number of ingredients times number of dishes, even if you only had two dishes that between them use only eight out of the 45 ingredients.
recipeTable = array2table(zeros(numel(ingredients_list_tot), 2),...
'variablenames',,,,["beef with broccoli",,,,"BLT with cheese"],...
'Rownames',Infredients_list_tot);
% Add recipes to the list
conteTable {[[126],"beef with broccoli"}= 1;% Steak, Broccoli, Teriyaki Sauce
conteTable {[["Tomato",,,,"Lettuce",,,,“培根”,,,,“奶酪片”,,,,"Bread"], 2} = 1;% BLT with cheese
%我'm including some ingredients not in either dish for demonstration of
% what the table looks like
disp(recipeTable([1 2 5 6 15 16 20 35 36 37], :))
我需要用奶酪制作BLT什么?
bltcheese = reperetable(reperetable。(("BLT with cheese")== 1,"BLT with cheese")
shopplist = bltcheese.properties.lownames