How do I assign result array of a loop to a variable name?

2 ビュー (過去 30 日間)
Struggling in MATLAB
Struggling in MATLAB 2022 年 7 月 28 日
I have a for loop which gives an one dimensional array in each iteration. (i.e. [1;2;3;4]) But they could be different in length. The example code is given below.
feature = ["intensityofcost1","intensityofcost2","intensityofcost3", ...
"rewardconcentration1","rewardconcentration2","rewardconcentration3", ...
"rewardconcentration4"];
uniqueFeature = {'uniqueCost1','uniqueCost2','uniqueCost3', ...
'uniqueReward1','uniqueReward2','uniqueReward3','uniqueReward4'};
for ii = 1:numel(feature)
% some code
uniqueFeature{ii} = unique(noNaNcleanedData);
end
I want to assign the result of each iteration to the variable names in 'uniqueFeature' cell array. How can I do it?

採用された回答

Steven Lord
Steven Lord 2022 年 7 月 28 日
Can you dynamically create variables with numbered names like uniqueCost1, uniqueCost2, uniqueCost3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
  5 件のコメント
Struggling in MATLAB
Struggling in MATLAB 2022 年 7 月 29 日
I think it would increase the readability of my script. Else I always have to refer back to the array name container uniqueFeature to see which variable I am dealing with.
Besides, I am generally curious about how people do it.
Struggling in MATLAB
Struggling in MATLAB 2022 年 7 月 29 日
Got it. Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by