
% Function: Determine the position of the element in array B corresponding to the equivalent element in array A
% Author:Gong Pf. Time:2019-7-2
% Remark: Only consider A and B is one or two dimension
% And x is a size(B,1)*size(B,2) cell
% Each cell x{i,j} is a t*2 array
% (t is the number of times that the element B(i,j) appears in A)
% And x{i,j}(n,1),x{i,j}(n,2) represent the row and column of the element in A
% Example1:A = [1 2 3 4 1 2 8]
% B = [0 1 2]
% x = argmax_min(A,B)
% x = [][2x2 double][2x2 double] = [] [1 11 5] [1 21 6]
% Example2:A = [1 2 42 4 25 9 83 9 3]
% B = [1 32 9]
% x = argmax_min(A,B)
% x = [1x2 double][2x2 double]= [1 1][4 14 3]
% [3x2 double][2x2 double] [1 22 12 3] [3 24 2]
% Interpret the result: (x{1,1}(1,1),x{1,1}(1,2))=(1,1)
%This is where B(1,1) appears in A
% (x{1,2}(1,1),x{1,2}(1,2))=(4,1) and (x{1,2}(2,1),x{1,2}(2,2))=(4,3)
% These are where B(1,2) appears in A
% Can be used to implement argmin and argmax functions
%% A and B array is less or equal two-dimensional
row_col = size(A)
ii = 1
jj = 1
% Count the number of occurrences of elements in one-dimensional array B in A
B_times = zeros(size(B))
for i = 1:size(B,1)
for j = 1:size(B,2)
B_times(i,j) = length(find(A == B(i,j)))
end
end
% Count the position of the element in B in A
for i = 1:size(B,1)
for j = 1:size(B,2)
for m = 1:row_col(1)
for n = 1:row_col(2)
if A(m,n) == B(i,j)
x{i,j}(1,ii) = m
x{i,j}(2,jj) = n
ii = ii + 1
jj = jj + 1
end
end
end
ii = 1
jj = 1
end
end
x=norm(y-ah,2)
x1=min(min(x))
x2=Corresponding_index(x,x1)
disp(x2)
y=λ*norm(h,1)
题主的matlab中的argmin函数可以这样来表示求解:1、自定义函数为目标函数,即
function y=argmin_fun(x)
xt=。。。
yt=。。。
zt=。。。
a=x(1)b=x(2)c=x(3)
y=(xt-a)^2+(yt-b)^2+(zt-c)^2-9.8^2
end
2、确定a、b、c的初值, 如x0=[0,0,0],根据问题而定
3、确定a、b、c的上限lb和下限ub
[x,fval] = fmincon(@(x) argmin_fun(x),x0,[],[],[],[],lb,ub)
a=x(1),b=x(2),c=x(3) %系数
fmin=fval %最小值
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)