Reputation: 11
I would like to solve a linear system AX = B where A is nxn matrix with constant elements, the matrix B is of type nx1. However, each element of the matrix B is a nx1 vector of type (this occurs because each element bij is time-varying).
%%% Example
t = 0:0.002:0.5; %% Time
A = [1 0 -1 0 0 0; ...
0 -1 0 0 1 0; ...
r12y, r32y-r12x r32x 0 0; ...
0 0 -1 0 1 0; ...
0 -1 0 0 1 0; ...
0 r23y-7-r43y r23x r43x];
%% Where rij is constant
% Construction 6x1 matrix C
C = [m2.*A2x ; ...
m2.*FG2-a2y; ...
ICM2.*Alpha2; ...
m3.*A3X ; ...
m3.*a3y-FG3; ...
Icm3.*Alph8a3];
%% Where A2x, a2y, A3X, a3y, alpha2, Alpha3 are elements of the matrix C that are time-varying.
I tried to solve the segunte form:
C = rand (6,1,251);
A = rand (6,6);%
X = zeros (6, size (C, 3));
for i = 1: size (C, 3)
X (:, i) = A \ C (:,:, i);
end
But I do not know if it's the best way.
Upvotes: 1
Views: 301
Reputation: 12099
You can just do the following.
C = reshape(rand(6,1,251), 6, 251); % Or just create rand(6, 251);
A = rand(6,6);
X = A \ C;
This will give you the same results and will be faster than the for loop.
Upvotes: 3