MATLAB program to split a given matrix A into two matrices B & C such that B+C=A.

4 vues (au cours des 30 derniers jours)
Sheet
Sheet le 11 Juil 2024
Commenté : Steven Lord le 11 Juil 2024
Hi,
Can you help me splitting a matrix A into B & C such that B+C=A? I need a code to generate all possible combinations of B & C.
For example, if A=[1 2 2 4;2 4 3 0;8 4 0 5;-3 4 5 0] then, B=[1 0 2 0;0 0 3 0;8 4 0 0;-3 0 0 0] and C=[0 2 0 4;2 4 0 0;0 0 0 5;0 4 5 0] are a pair of suitable combinations. I need all possible combinations in which (almost) half of the non zero elements are in B and remaining are in C.
Thank you.
  4 commentaires
Sheet
Sheet le 11 Juil 2024
20x20 in which half of the elements are zeros.
Steven Lord
Steven Lord le 11 Juil 2024
So you have a matrix with 200 non-zero elements
numNonzeros = 20*20*(1/2)
numNonzeros = 200
and you want to choose a set of roughly 100 of those to be in one of the matrix A and the remainder in the matrix B. How many ways is there to choose the set of elements to be in A (since you say you want all possible A matrices) if we just consider the case where we exactly split the set of non-zero elements in half?
numCombinations = nchoosek(numNonzeros, numNonzeros/2)
Warning: Result may not be exact. Coefficient has a maximum relative error of 4.0856e-14, corresponding to absolute error 3.699468886058764e+45.
numCombinations = 9.0549e+58
Even if we divide that by two (if you consider the case where A has non-zero elements 1 through 100 and B has 101 through 200 to be the same as the case where A has 101 through 200 and B has 1 through 100) that's still a huge number. Let's say you processed a million of those combinations a second. How long would it take to get through your list?
time = years(seconds(numCombinations/1e6))
time = 2.8694e+45
That doesn't consider the cases where A has 101 non-zero elements and B 99, the case where A has 102 and B 98, etc.
Whatever you're hoping to do with this (very, very, very large) set of matrices is not going to be feasible. If you're hoping to brute force the solution to a problem by examining each combination you should rethink your approach to solving that problem. If you tell us what problem you're trying to solve we may be able to suggest an alternate technique to solve it.

Connectez-vous pour commenter.

Réponses (1)

Hassaan
Hassaan le 11 Juil 2024
% Given matrix A
A = [1 2 2 4; 2 4 3 0; 8 4 0 5; -3 4 5 0];
% Find the non-zero elements and their indices
[rows, cols, values] = find(A);
% Number of non-zero elements
numNonZero = length(values);
% Randomly shuffle the indices of non-zero elements
randIndices = randperm(numNonZero);
% Split the shuffled indices into two approximately equal halves
half = floor(numNonZero / 2);
B_indices = randIndices(1:half);
C_indices = randIndices(half+1:end);
% Initialize B and C as zero matrices of the same size as A
B = zeros(size(A));
C = zeros(size(A));
% Assign the elements to B and C
for i = 1:half
B(rows(B_indices(i)), cols(B_indices(i))) = values(B_indices(i));
end
for i = 1:(numNonZero - half)
C(rows(C_indices(i)), cols(C_indices(i))) = values(C_indices(i));
end
% Display the matrices
disp('Matrix A:');
Matrix A:
disp(A);
1 2 2 4 2 4 3 0 8 4 0 5 -3 4 5 0
disp('Matrix B:');
Matrix B:
disp(B);
1 2 0 4 0 0 3 0 0 0 0 0 -3 4 0 0
disp('Matrix C:');
Matrix C:
disp(C);
0 0 2 0 2 4 0 0 8 4 0 5 0 0 5 0
disp('B + C:');
B + C:
disp(B + C);
1 2 2 4 2 4 3 0 8 4 0 5 -3 4 5 0
% Verify that B + C equals A
assert(isequal(A, B + C), 'B + C does not equal A');

Catégories

En savoir plus sur Portfolio Optimization and Asset Allocation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by