Find the Minimum Combination of Sum

11 visualizaciones (últimos 30 días)
MarshallSc el 19 de En. de 2022
Comentada: MarshallSc el 19 de En. de 2022
I have a 100 x 1 vector with numeric values, what I want to find is the minimum combination of the reference point with the upper and lower indices for the equation below. For example:
A = rand(100,1);
%Lets say the reference point is A(31)
comb = +-A(31 + 1) +- A(31) +- A(31-1) % So there will be 2^3 scenarios
% When the reference point reaches index 100 (length of the vector), the operation stops
I want to find the absolute minimum combintation of all the possible combinations. Can a function be created to perform this operation since I need to perform this for multiple vectors in a for loop?
2 comentariosMostrar NingunoOcultar Ninguno
Voss el 19 de En. de 2022
Editada: Voss el 19 de En. de 2022
For a given reference point, e.g., A(31), the minimum combination will be the one where all the numbers added together are negative (or zero), i.e., if A(32) is positive, then subtract it (i.e., pick "-"), if A(31) is negative, then add it (i.e., pick "+"), etc., so you'll always be adding three negative (or zero-valued) numbers. Is this what you want?
Or do you want the combination with the minimum absolute value, i.e., the combination closest to zero?
MarshallSc el 19 de En. de 2022
Thanks for your reply. Yes, I want the minimum absolute value which is the closest combination to zero.

Iniciar sesión para comentar.

Voss el 19 de En. de 2022
N = 100;
window_size = 3;
A = rand(N,1);
signs = 1-2*(dec2bin(0:2^(window_size-1)-1,window_size)-'0').';
comb = NaN(N,1);
comb_type = NaN(N,1);
d = (window_size-1)/2;
for i = d+1:N-d
[comb(i),comb_type(i)] = min(abs(sum(A(i+(-d:d)).*signs)));
end
[min_comb,idx] = min(comb);
% minimum absolute-value sum:
disp(min_comb);
0.0041
% occurs at this row of A:
disp(idx);
17
% using these elements of A:
disp(A(idx+(-d:d)));
0.8604 0.8003 0.0643
% with this way of combining the elements:
disp(signs(:,comb_type(idx)));
1 -1 -1
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Más respuestas (1)

Jon el 19 de En. de 2022
If I understand what you are trying to do, I think this might work for you
A = rand(100,1);
% define matrix which can be multiplied by 3 elements to give all possible
% signed summations
c = [-1 -1 -1;-1 -1 1;-1 1 -1; -1 1 1;1 -1 -1;1 -1 1;1 1 -1; 1 1 1]
% loop through data
% not sure what you want to do about end points, so start and end on
% interior points
numPoints = numel(A)
result = zeros(numPoints-2,1); % preallocate array to hold result
for k = 2:numPoints-1
result(k) = min(abs(c*A(k-1:k+1)));
end
2 comentariosMostrar NingunoOcultar Ninguno
Jon el 19 de En. de 2022
Actually since you are taking the absolute value you actually only need the first 4 rows of c
MarshallSc el 19 de En. de 2022
Thank you sir, this method is very useful.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by