Cells and Matrix division

2 visualizzazioni (ultimi 30 giorni)
Filip Wadman
Filip Wadman il 22 Nov 2021
Modificato: DGM il 22 Nov 2021
Hi,
I have a problem where I want to find the solution to A*x=B
Where A is a matrix of 4x4 where each element in the matrix is of the size 1x10. B is a matrix 4x1 where each element is of the size 1x10. The x matrix should be a matrix 4x1 where each element is of the size 4x1 where each element is the size of 1x10. How can I succed with this?
If needed I can write a code with exemples of the matrices.
  2 Commenti
Star Strider
Star Strider il 22 Nov 2021
Simply out of curiosity, find a solution to what problem?
Filip Wadman
Filip Wadman il 22 Nov 2021
Hi I have updated it now

Accedi per commentare.

Risposte (2)

Filip Wadman
Filip Wadman il 22 Nov 2021
Hi I updated my text to make it better.

DGM
DGM il 22 Nov 2021
Modificato: DGM il 22 Nov 2021
I'm still assuming you're using cell arrays, since that's the only way that the description makes sense.
I'm sure someone can whip up a more elegant solution, maybe even directly dealing with the cell array without converting it. ... but this is what I did as a simplified example.
sa = 4; % size of system
nsys = 10; % number of systems
% generate a test array (all coefficient matrixes)
A = repmat(randi([-100 100],sa,sa),[1 1 nsys]);
% rearrange to mimic the array you have
A = cellfun(@(x) permute(x,[1 3 2]),num2cell(A,3),'uniform',false)
A = 4×4 cell array
{[95 95 95 95 95 95 95 95 95 95]} {[100 100 100 100 100 100 100 100 100 100]} {[ 40 40 40 40 40 40 40 40 40 40]} {[ 36 36 36 36 36 36 36 36 36 36]} {[22 22 22 22 22 22 22 22 22 22]} {[ 39 39 39 39 39 39 39 39 39 39]} {[ 44 44 44 44 44 44 44 44 44 44]} {[-46 -46 -46 -46 -46 -46 -46 -46 -46 -46]} {[43 43 43 43 43 43 43 43 43 43]} {[-82 -82 -82 -82 -82 -82 -82 -82 -82 -82]} {[-30 -30 -30 -30 -30 -30 -30 -30 -30 -30]} {[ 40 40 40 40 40 40 40 40 40 40]} {[37 37 37 37 37 37 37 37 37 37]} {[-68 -68 -68 -68 -68 -68 -68 -68 -68 -68]} {[ 17 17 17 17 17 17 17 17 17 17]} {[-18 -18 -18 -18 -18 -18 -18 -18 -18 -18]}
% turn it back into a regular array
A = cell2mat(cellfun(@(x) permute(x,[1 3 2]),A,'uniform',false))
A =
A(:,:,1) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,2) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,3) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,4) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,5) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,6) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,7) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,8) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,9) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18 A(:,:,10) = 95 100 40 36 22 39 44 -46 43 -82 -30 40 37 -68 17 -18
% all b vectors as you describe
b = repmat({repmat(10,[1 nsys])},[sa 1]);
% turn it into a regular array
b = cell2mat(b)
b = 4×10
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
x = zeros(sa,nsys);
for k = 1:nsys
x(:,k) = A(:,:,k)\b(:,k);
end
x % the answers
x = 4×10
0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669
% turn it back into an unweildy cell array??
x = num2cell(x,2)
x = 4×1 cell array
{[ 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316 0.4316]} {[ 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755 0.0755]} {[-0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436 -0.5436]} {[-0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669 -0.4669]}

Community Treasure Hunt

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

Start Hunting!

Translated by