How do I assign result array of a loop to a variable name?
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!