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

1 回表示 (過去 30 日間)
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?

回答 (1 件)

parham kianian
parham kianian 2020 年 5 月 12 日
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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by