How to delete some of anonymous variables randomly?

1 ビュー (過去 30 日間)
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 10 月 17 日
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);

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

採用された回答

Jeff Miller
Jeff Miller 2021 年 10 月 17 日
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.

その他の回答 (1 件)

KSSV
KSSV 2021 年 10 月 17 日
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 件のコメント
Asghar AmaniDashlejeh
Asghar AmaniDashlejeh 2021 年 10 月 20 日
The problem was solved by putting i in x(i) depending on double array in addition to multiplying double array to variables as shown below:
h_ctff=@(x) 0; h_ctfff=@(x) 0; h_ctf=@(x) 0; h_ccff=@(x) 0; h_ccfff=@(x) 0; h_ccf=@(x) 0; g=@(x) 0;
aa=0; a=1; bb=0; b=1; cc=0; c=1; dd=0; d=1; ee=0; e=1; ff=0; f=1; hh=0; h=1;
ll=0; l=1;
for iii=1:l_uu
switch uu(iii)
case 8
aa=1;
a=iii;
case 14
bb=1;
b=iii;
case 19
cc=1;
c=iii;
case 24
dd=1;
d=iii;
case 28
ee=1;
e=iii;
case 34
ff=1;
f=iii;
case 39
hh=1;
h=iii;
case 44
ll=1;
l=iii;
end
end
h_ctff=@(x) h_ctff(x)+(-((aa.*x(:,a+2).*(aa.*x(:,a).*aa.*x(:,a+1))+bb.*x(:,b+2).*bb.*(x(:,b).*bb.*x(:,b+1))+cc.*x(:,c+1).*...
(nb_btf.*pi.*cc.*x(:,c).^2./4)+dd.*x(:,d+1).*(nb_bcf.*pi.*dd.*x(:,d).^2./4)+ee.*x(:,e+2).*ee.*...
(x(:,e).*ee.*x(:,e+1))+ff.*x(:,f+2).*ff.*(x(:,f).*ff.*x(:,f+1))+hh.*x(:,h+1).*(nb_bts.*pi.*hh.*...
x(:,h).^2./4)+ll.*x(:,l+1).*(nb_bcs.*pi.*ll.*x(:,l).^2./4)).*alpha.*(x(:,7)./x(:,5))+(x(:,5).*...
((x(:,1).*n_h).*(x(:,2).*n_v)).*(alpha.^2.*(x(:,7)./x(:,5)).^2+(1-mmm).*(x(:,6)./x(:,5)).*(alpha...
.*(x(:,7)./x(:,5))+(x(:,6)./x(:,5)))))/((alpha.*(x(:,7)./x(:,5)))))+ sqrt(((aa.*x(:,a+2).*(aa.*x(:,a)...
.*aa.*x(:,a+1))+bb.*x(:,b+2).*bb.*(x(:,b).*bb.*x(:,b+1))+cc.*x(:,c+1).*(nb_btf.*pi.*cc.*x(:,c).^2./4)...
+dd.*x(:,d+1).*(nb_bcf.*pi.*dd.*x(:,d).^2./4)+ee.*x(:,e+2).*(ee.*x(:,e).*ee.*x(:,e+1))+ff.*x(:,f+2)...
.*ff.*(x(:,f).*ff.*x(:,f+1))+hh.*x(:,h+1).*(nb_bts.*pi.*hh.*x(:,h).^2./4)+ll.*x(:,l+1).*(nb_bcs.*pi...
.*ll.*x(:,l).^2./4)).*alpha.*(x(:,7)./x(:,5))+(x(:,5).*((x(:,1).*n_h).*(x(:,2).*n_v)).*(alpha...
.^2.*(x(:,7)./x(:,5)).^2+(1-mmm).*(x(:,6)./x(:,5)).*(alpha.*(x(:,7)./x(:,5))+(x(:,6)./x(:,5)))))./...
((alpha.*(x(:,7)./x(:,5))))).^2-4.*((x(:,5).*((x(:,1).*n_h).*(x(:,2).*n_v)).*(mmm-1).*((alpha...
.*(x(:,7)./x(:,5))+(x(:,6)./x(:,5)))).^2)./(2.*alpha.*(x(:,7)./x(:,5)).*(x(:,2).*n_v))).*(-((aa.*...
x(:,a+2).*(aa.*x(:,a).*aa.*x(:,a+1)).*aa.*x(:,a+4))+(bb.*x(:,b+2).*bb.*(x(:,b).*bb.*x(:,b+1)).*bb.*x(:,b+4))+...
(cc.*x(:,c+1).*(nb_btf.*pi.*cc.*x(:,c).^2./4).*cc.*x(:,c+3))+(dd.*x(:,d+1).*(nb_bcf.*pi.*dd.*x(:,d)...
.^2./4).*dd.*x(:,d+3))+(ee.*x(:,e+2).*ee.*(x(:,e).*ee.*x(:,e+1)).*ee.*x(:,e+4))+(ff.*x(:,f+2).*(ff.*...
x(:,f).*ff.*x(:,f+1)).*ff.*x(:,f+4))+(hh.*x(:,h+1).*(nb_bts.*pi.*hh.*x(:,h).^2./4).*hh.*x(:,h+3))+(...
ll.*x(:,l+1).*(nb_bcs.*pi.*ll.*x(:,l).^2./4).*ll.*x(:,l+3))).*alpha.*(x(:,7)./x(:,5))+(x(:,5).*(...
(x(:,1).*n_h).*(x(:,2).*n_v)).*(x(:,2).*n_v).*((mmm-1).*(x(:,6)./x(:,5)).^2-alpha.^2.*(x(:,7)./...
x(:,5)).^2))./(2.*alpha.*(x(:,7)./x(:,5))))))./(2.*((x(:,5).*((x(:,1).*n_h).*(x(:,2).*n_v)).*...
(mmm-1).*((alpha.*(x(:,7)./x(:,5))+(x(:,6)./x(:,5)))).^2)./(2.*alpha.*(x(:,7)./x(:,5)).*(x(:,2).*...
n_v))));

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by