How to vectorize this simple problem?

1 回表示 (過去 30 日間)
Mr M.
Mr M. 2017 年 5 月 10 日
編集済み: Matt J 2017 年 5 月 10 日
Let R(n) be a vector, f(r) and g(r) are functions. Let A and B are complementer sets of indices. For example A = R(R>0); B = R(R<=0); I would like to act f on the subvector R(A), and g on the subvector R(B), but get back the whole vector in the original order, so for the output vector V(n) = f(R(n)) if n is element of A, and V(n) = g(R(n)) if n is element of B. f and g are computationally complex functions, so I dont want to evaluate f and g on the whole input vector R, just on necessary elements, but using a vectorized method, and somehow get back automatically the vector in the original order, not to vaste time of index shuffling. Is it possible? Which is the best method?

採用された回答

Matt J
Matt J 2017 年 5 月 10 日
編集済み: Matt J 2017 年 5 月 10 日
V=nan(size(R));
V(A)=f(R(A));
V(B)=g(R(B));

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by