How do I avoid creating intermediate variables?
3 ビュー (過去 30 日間)
古いコメントを表示
Say I have a vector of data x and a function f(x).
Now say I want to evaluate f(x) only for x>2, so I do:
ind=x>2; y=f(x(ind));
Now say I want to find y>5;
ind2=y>5;
Now how can I use this information to get the x values corresponding to the y>5 values, WITHOUT using this crude method where I create an intermediate variable:
x_intermediate = x(ind1); % I want to avoid creating this
x_answer = x_intermediate(ind2);
0 件のコメント
採用された回答
その他の回答 (2 件)
Matt Fig
2011 年 5 月 30 日
Is your complaint about the extra typing, or the extra variable in the workspace? If the latter, then one way to avoid this kind of thing in general is:
x_answer = x(ind1); % This will be overwritten.
x_answer = x_answer(ind2);
This strategy will serve you beyond this specific problem.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!