Storing multiple matrices from a loop into a single variable without cell function
Afficher commentaires plus anciens
Hello guys I need help in storing the A matrix for the loop at each iteration. I want to know if I can store this A matrix without using the cell function. Here is the code:
clear
clc
clear all
n = 26;
q1(1) = 0.002*n;
q2(1) = 0.001*n;
q3(1) = 0.005*n;
q4(1) = sqrt(1-(q1(1)).^2-(q2(1)).^2-(q3(1)).^2);
wx(1) = 0.0002+0.0001*n;
wy(1) = 0.0003+0.0001*n;
wz(1) = 0.0004+0.0001*n;
jx = 2.1*10^-3;
jy = 2*10^-3;
jz = 1.9*10^-3;
delta_t = 0.1;
Nt = 3.6*10^-10;
t(1) = 0;
phi(1) = atan((2*(q2(1)*q3(1)+q1(1)*q4(1)))/(1-2*(q1(1).^2+q2(1).^2)));
theta(1) = asin(2*(q4(1)*q2(1)-q1(1)*q3(1)));
ksi(1) = atan((2*(q4(1)*q3(1)+q1(1)*q2(1)))/(1-2*(q2(1).^2+q3(1).^2)));
i = 1;
n_iteration = 54000;
while i<= n_iteration
t(i+1)= t(i)+delta_t;
wx(i+1) = wx(i)+(delta_t/jx)*(jy-jz)*wz(i)*wy(i)+(delta_t/jx)*Nt;
wy(i+1) = wy(i)+(delta_t/jy)*(jz-jx)*wx(i)*wz(i)+(delta_t/jy)*Nt;
wz(i+1) = wz(i)+(delta_t/jz)*(jx-jy)*wx(i)*wy(i)+(delta_t/jz)*Nt;
q1(i+1) = q1(i)-0.5*delta_t*(q2(i)*wx(i)+q3(i)*wy(i)+q4(i)*wz(i));
q2(i+1) = q2(i)+0.5*delta_t*(q1(i)*wx(i)-q4(i)*wy(i)+q3(i)*wz(i));
q3(i+1) = q3(i)+0.5*delta_t*(q4(i)*wx(i)+q1(i)*wy(i)-q2(i)*wz(i));
q4(i+1) = q4(i)-0.5*delta_t*(q3(i)*wx(i)-q2(i)*wy(i)-q1(i)*wz(i));
phi(i) = atan((2*(q2(i)*q3(i)+q1(i)*q4(i)))/(1-2*(q1(i).^2+q2(i).^2)));
theta(i) = asin(2*(q4(i)*q2(i)-q1(i)*q3(i)));
ksi(i) = atan((2*(q4(i)*q3(i)+q1(i)*q2(i)))/(1-2*(q2(i).^2+q3(i).^2)));
A1_1(i) = cos(theta(i)).*cos(ksi(i));
A1_2(i) = cos(theta(i)).*sin(ksi(i));
A1_3(i) = -sin(theta(i));
A2_1(i) = -cos(phi(i)).*sin(ksi(i))+sin(phi(i)).*sin(theta(i)).*cos(ksi(i));
A2_2(i) = cos(phi(i)).*cos(ksi(i))+sin(phi(i)).*sin(theta(i)).*sin(ksi(i));
A2_3(i) = sin(phi(i)).*cos(theta(i));
A3_1(i) = sin(phi(i)).*sin(ksi(i))+cos(phi(i)).*sin(theta(i)).*cos(ksi(i));
A3_2(i) = -sin(phi(i)).*cos(ksi(i))+cos(phi(i)).*sin(theta(i)).*sin(ksi(i));
A3_3(i) = cos(phi(i)).*cos(theta(i));
i=i+1
end
Réponses (1)
Chendi Lin
le 9 Avr 2021
Hi Ilker,
If I understand your question correctly, A is a 3x3 matrix. For each i in the iteration, you want to store A without using cell.
Have you tried to store everything in a 3D matrix? For example,
As = zeros(n_iteration,3,3);
while i<= n_iteration
As[i,:,:] = A;
end
Best,
CD
7 commentaires
Walter Roberson
le 9 Avr 2021
As = zeros(n_iteration,3,3);
while i<= n_iteration
%compute A
As(i,:,:) = A;
i=i+1;
end
for loop would be more natural though.
Ilker Enes Çirkin
le 9 Avr 2021
Chendi Lin
le 9 Avr 2021
As = zeros(n_iteration,3,3) initializes a 3D matrix. You can imagine that each As[i] is a 3x3 matrix, which matches the dimension of A.
I assume what you want is
As = zeros(n_iteration,3,3);
for i = 1:n_iteration
%compute A
As(i,1,1) = A1_1;
As(i,1,2) = A1_2;
... etc
i=i+1;
end
Ilker Enes Çirkin
le 9 Avr 2021
Chendi Lin
le 9 Avr 2021
Sorry that I was probably not clear about the modified code. In my code, A1_1 is a scalar value, while in your original code, A1_1 is a vector containing all values. If you try
As = zeros(n_iteration,3,3);
for i = 1:n_iteration
%compute A
A1_1 = cos(theta(i)).*cos(ksi(i));
A1_2 = cos(theta(i)).*sin(ksi(i));
As(i,1,1) = A1_1;
As(i,1,2) = A1_2;
... etc
i=i+1;
end
Would that give you the correct answer?
Ilker Enes Çirkin
le 9 Avr 2021
Walter Roberson
le 9 Avr 2021
I think you will find that the code works much better if you use 3D indexing such as A(i,:,:) = [that array]
You can reshape() or permute() afterwards .
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!