How to delete some of anonymous variables randomly?
1 view (last 30 days)
Asghar AmaniDashlejeh on 16 Oct 2021
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?
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.
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.
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 ;