Clear Filters
Clear Filters

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

47 views (last 30 days)
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 Comments
Steven Lord
Steven Lord on 11 Jul 2024 at 12:28
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.

Sign in to comment.

Answers (1)

Hassaan
Hassaan on 11 Jul 2024 at 10:07
% 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');

Community Treasure Hunt

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

Start Hunting!