How to save results matrix in multi dimensional matrix?

1 view (last 30 days)
I have A matrix of 9x9, B matrix of 9x1.
C is my result matrix of 9x1
I have two for loops as follows where t and e are variables used in the above mentioned matrices.
for t=25:10:45
for e=0.25:0.1:0.45
C = inv(A)*B
end
end
Now, I want result matrices for all t in first matrix ((9x1) which will be (9x9) matrix) for first variable of c and second and third (9x9)martices for second and third values of c.
.I dont know how to do. May I get help in this?
  2 Comments
Jan
Jan on 27 Feb 2023
I do not understand this sentence: "result matrices for all t in first matrix ((9x1) which will be (9x9) matrix) for first variable of c and second and third (9x9)martices for second and third values of c." What is the "first matrix"? Isn't "(9x1) which will be (9x9) matrix" a contradiction?
Where are A and B defined?
Note that inv(A)*B is less stable than A\B, so prefer the last.
Vignesh
Vignesh on 27 Feb 2023
A matrix 9x9 and B matrix 9x1 is established before the code.I didn't posted to reduce the cluster.
It is linear solving of the two matrix for 9 variables, so that the resultant matrix C will be 9x1.
t and e are the two variables which comes in the matrices A and B which need to be assigned values.So, two far loops are assigned for the same.
Now, if the variables t and e are assigned a single variables each, then my result matrix C is going to be 9x1 matrix. But, as I am varying those values in the loop, I want to store the result matrices in multi-dimensional for easy understanding.
So, I would like to store the result matrix of first iteration of t value in first matrix 9rows x 1st column, then second iteration of t value in first matrix 9rows x 2nd column etc,. until the t loop values for first e value ends. Then, I want to save a new matrix for all t iterations for second values of e etc,.
Hope I am clear now.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 27 Feb 2023
Edited: Stephen23 on 27 Feb 2023
In general with MATLAB it is easier (even if more verbose) to iterate over indices rather than over data. For example:
tV = 25:10:45;
eV = 0.25:0.1:0.45;
tN = numel(tV);
eN = numel(eV);
C = cell(tN,eN);
for ii = 1:tN
for jj = 1:eN
t = tV(ii);
e = eV(jj);
.. define your matrices here
C{ii,jj} = A\B; % better than INV(A)*B
end
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!