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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by