How to delete some of anonymous variables randomly?
1 view (last 30 days)
Show older comments
Asghar AmaniDashlejeh
on 16 Oct 2021
Commented: Asghar AmaniDashlejeh
on 20 Oct 2021
Hello Everyone,
I have a anonymous nonlinear function with 47 variables (f=x(:,1)+x(:2).*x(:,3)./x(:,4)+...x(:,47)). In each iteration, I need some of the variables, randomly (for example x(:,1), x(:.25), and x(:,34) in the first iteration), and f should be calculated only according to these variables. Also, the number of the selected variables is important, and I need to delete the other variables (not putting zero) in each iteration.
How can I do that?
4 Comments
Walter Roberson
on 17 Oct 2021
Would it be correct that your f is a sum of subexpressions, and that at any particular time you want to randomly select a subset of variables, and you want to create a new related function that removes from the sum any term that involves a variable that is not selected?
This would be different than setting the variables to 0 because you show us an example where you divide by x(:,4) . If I understand correctly, if x(2) and x(3) happened to be selected by x(:,4) did not happen to be selected, then you would want to delete the entire term x(:,2).*x(:,3)./x(:,4) ?
If so... then do you have access to the Symbolic toolbox? And can all terms of f() be evaluated symbolically?
Walter Roberson
on 17 Oct 2021
Also, is this always going to be one specific equation that could be analyzed by hand?
nrow = size(x,1);
involves = {[1], [2 3 4], [5 8], ...} %hardcoded analysis!
subexpr = zeros(nrow,length(involves));
if all(active(involves{1}))
subexpr(:,1) = x(:,1);
end
if all(active(invokes{2}))
subexpr(:,2) = x(:,2).*x(:,3)./x(:,4);
end
...
etc
output = sum(subexpr,2);
Accepted Answer
Jeff Miller
on 17 Oct 2021
One way to approach the problem is to write a function f(x) where x is a vector with 47 positions. When you call f, put numerical values in the positions corresponding to the x's that you want to use, and put nan's in the positions corresponding to the x's that you want to leave out.
The tricky part is that the function f will need some logic to figure out how to compute its final value from the x's that are supplied, ignoring the x's that are specified as nan. That logic could be pretty simple or extremely complex, depending on exactly how each of the different x's is (potentially) used to compute the function. If it is a sum of subexpressions, as Walter suggested, then it should not be too bad.
0 Comments
More Answers (1)
KSSV
on 17 Oct 2021
Multiply each variable with a constant array, and assign this contant array with 0 and 1. See to it that, randomly only three 1's are present and rest are zero, so that thought you substitue the variables, the respective contribution will be zero.
Example:
K = zeros(1,4) ; % initiate K
idx = randperm(4,2) ; % Make randomly one
K(idx) = 1 ;
f = K(1)*X1+K(2)*X2+K(3)*X3/X4+K(4)*X5 ;
3 Comments
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!