MATLAB program to split a given matrix A into two matrices B & C such that B+C=A.
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Steven Lord
il 11 Lug 2024
So you have a matrix with 200 non-zero elements
numNonzeros = 20*20*(1/2)
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)
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))
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.
Risposte (1)
Hassaan
il 11 Lug 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:');
disp(A);
disp('Matrix B:');
disp(B);
disp('Matrix C:');
disp(C);
disp('B + C:');
disp(B + C);
% Verify that B + C equals A
assert(isequal(A, B + C), 'B + C does not equal A');
0 Commenti
Vedere anche
Categorie
Scopri di più su Portfolio Optimization and Asset Allocation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!