フィルターのクリア

A clever way to have dynamic output variables from a function??

11 ビュー (過去 30 日間)
Jack Latham
Jack Latham 2019 年 7 月 11 日
編集済み: Stephen23 2019 年 7 月 11 日
I have a function which creates a feature matrix with ~1500 different features, I'd like to pass an index of selected features to this function so only the features of interest are calculated (this index of selected features will be changing so I don't want to hard code anything). I'm hoping someone can suggest a clever way of doing this? For example, how could I use FSi (Feature selection index) in the following:
function Features = getFeatures(Xin, FSi)
F1 = someFunction(Xin);
F2 = someFunction2(Xin);
....
F1500 = someFunction1500(Xin)
Features = [F1, F2,..,F1500]
end
  1 件のコメント
Stephen23
Stephen23 2019 年 7 月 11 日
編集済み: Stephen23 2019 年 7 月 11 日
Any time that you find yourself putting indices (or any other meta-data) into variable names then this is a BIG FLASHING SIGN telling you that you are doing something wrong:
And yet you were so close to the simpler, neater, much more efficient solution using indexing: your code shows that you want the output as one array, so skip the intermediate variables entirely and simply allocate directly to the output array using indexing. This will be much simpler and much more efficient than anything that involves numbered variables/functions.

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

回答 (1 件)

dpb
dpb 2019 年 7 月 11 日
Do NOT write code with metadata in variable names nor sequentially-numbered or lettered ones for starters...use an array. Then it's trivial...
function Features = getFeatures(Xin, FSi)
Features=zeros(NFEATURES); % NFEATURES constant of output array size needs setting!!!
for i=1:NFEATURES
Features(1) = fn{i}(Xin);
end
Features = Features(FSi);
end
In the above, fn is an array of anonymous function handles; it also must be built and loaded.
  1 件のコメント
Jack Latham
Jack Latham 2019 年 7 月 11 日
Thanks, using function handles is a good suggestion

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by