How to pick random entries from the columns of a matrix such that their sum is equal to a already specified number?

1 visualización (últimos 30 días)
An example to clarify my question is that Let's assume a matrix
A=[ 0.8147 0.9134; 0.9058 0.6324; 0.1270 0.0975];
I want to select entries from each column such that their sum is always equal or approximately equal to a number say 1 for the above matrix. How can I do this?

Respuestas (1)

parham kianian
parham kianian el 12 de Mayo de 2020
I suppose you want to perform this on each column of matrix A seperately. Try this:
A=[ 0.8147 0.9134; 0.9058 0.6324; 0.1270 0.0975];
v = nchoosek(A(:,1),2); %All possible selection of two number out of three for first column of A
%each row of matrix v shows one possible selection
x = abs(sum(v,2) - 1); %sum arrays of each row of matrix v then substract it by 1 and take absolute value
%suppose desired solution is where the sum lies between 0.9 and 1.1
r = find(x == min(x));
best_selection = v(r,:); %best selection of all possible selections of two number out of three
You can develop a for loop to perform on each column of matrix A or find a way to vectorize the process to enhance code performance if matrix A has large dimension.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by