All my Values not getting stored. Only my Last Value is getting stored

4 vues (au cours des 30 derniers jours)
Tishan Anantharajah
Tishan Anantharajah le 13 Mar 2023
Hi All,
Below attached is my code. In this code I am trying to produce various iterations of theta however only the last value of theta is getting stored in the loop. How can I fix this? Furthermore I am trying to save it into an excel spreadsheet however as it is only producing the last value, only one value is stored.
Thank You,
  2 commentaires
Cameron
Cameron le 13 Mar 2023
Which variable are you trying to store? theta_average?
Tishan Anantharajah
Tishan Anantharajah le 13 Mar 2023
Yes I am trying to store theta_average

Connectez-vous pour commenter.

Réponses (2)

Matt J
Matt J le 13 Mar 2023
Modifié(e) : Matt J le 13 Mar 2023
A loop will not accumulate a vector of results unless you tell it where in the vector the result is to be placed, e.g.,
for i=1:5
theta(i)=i^2;
end
theta
theta = 1×5
1 4 9 16 25
Compare this to,
for i=1:5
alpha=i^2;
end
alpha
alpha = 25
  2 commentaires
Tishan Anantharajah
Tishan Anantharajah le 13 Mar 2023
How would my code be changed accordingly to accumulate this?
Matt J
Matt J le 13 Mar 2023
Modifié(e) : Matt J le 13 Mar 2023
As in my example above. You currently have no indexing on the left hand side of your assignment statements that tell where your data is to be stored.

Connectez-vous pour commenter.


Cameron
Cameron le 13 Mar 2023
You need to save values of theta_average, so you can index it this way. You go through a while loop then save it when you need to by indexing.
k = 0 ;
theta_average = []; %preallocate this variable, then save it at the end
while true
hold on
va = 1;
mu = 0;
a = rand([3,1]);
b = rand([3,1]);
c = rand([3,1]);
d = rand([3,1]);
a_real = (1/sqrt(2*pi)*exp((-a.^2)/2));
b_real = (1/sqrt(2*pi)*exp((-b.^2)/2));
c_real = (1/sqrt(2*pi)*exp((-c.^2)/2));
d_real = (1/sqrt(2*pi)*exp((-d.^2)/2));
v1 = b_real-a_real;
v2 = c_real-a_real;
v3 = c_real-b_real;
v4 = d_real-c_real;
t = linspace(0,1);
v1x = v1(1)*t + a_real(1) ; v1y = v1(2)*t + a_real(2); v1z = v1(3)*t + a_real(3);
v2x = v2(1)*t + b_real(1) ; v2y = v2(2)*t + b_real(2); v2z = v2(3)*t + b_real(3);
v3x = v3(1)*t + c_real(1) ; v3y = v3(2)*t + c_real(2); v3z = v3(3)*t + c_real(3);
v4x = v4(1)*t + d_real(1) ; v4y = v4(2)*t + d_real(2); v4z = v4(3)*t + d_real(3);
grid on; rotate3d on
plot3(a_real(1),a_real(2),a_real(3),'or')
hold on
plot3(b_real(1),b_real(2),b_real(3),'or')
plot3(c_real(1),c_real(2),c_real(3),'or')
plot3(d_real(1),d_real(2),d_real(3),'or')
plot3(v1x,v1y,v1z,'b','linew',2)
plot3(v2x,v2y,v2z,'b','linew',2)
plot3(v3x,v3y,v3z,'b','linew',2)
plot3(v4x,v4y,v4z,'b','linew',2)
% Only 6 variations are possible for 4 points.
n_t = cross(v1,v2); n = n_t/norm(n_t);
% Did not cross v2 and v3 because they produce the same normal.
n2 = cross(v1,v3); n2 = n2/norm(n2);
% Why is n3 so large? How should I pick which normal to use out of a
% combination?
n3 = cross(v1,v4); n3 = n3/norm(n3);
n4 = cross(v2,v4); n4 = n4/norm(n4);
n5 = cross(v3,v4); n5 = n5/norm(n5);
n_average = (n3+n4+n5)./3;
nx = n(1)*t + a_real(1) ; ny = n(2)*t + a_real(2); nz = n(3)*t + a_real(3);
n3x = n3(1)*t + a_real(1) ; n3y = n3(2)*t + a_real(2); n3z = n3(3)*t + a_real(3);
n4x = n4(1)*t + a_real(1) ; n4y = n4(2)*t + a_real(2); n4z = n4(3)*t + a_real(3);
n5x = n5(1)*t + a_real(1) ; n5y = n5(2)*t + a_real(2); n5z = n5(3)*t + a_real(3);
n_averagex = n_average(1)*t + a_real(1) ; n_averagey = n_average(2)*t + a_real(2); n_averagez = n_average(3)*t + a_real(3);
plot3(nx,ny,nz,'r','LineWidth',2)
plot3(n3x,n3y,n3z,'r','LineWidth',2)
plot3(n4x,n4y,n4z,'r','LineWidth',2)
plot3(n5x,n5y,n5z,'r','LineWidth',2)
plot3(n_averagex,n_averagey,n_averagez,'r','LineWidth',2)
% Why is this not plotting?
[x,y] = meshgrid(1:100:8000);
% plane = n(1)(x-a(1)) + n(2)(y-a(2)) + n(3)(z-a(3)) = 0
z = ((n(1).*(x-a_real(1))) + (n(2).*(y-a_real(2))) - (n(3)*a_real(3)))./-(n(3));
z3 = ((n3(1).*(x-a_real(1))) + (n3(2).*(y-a_real(2))) - (n3(3)*a_real(3)))./-(n3(3));
z4 = ((n4(1).*(x-a_real(1))) + (n4(2).*(y-a_real(2))) - (n4(3)*a_real(3)))./-(n4(3));
z5 = ((n5(1).*(x-a_real(1))) + (n5(2).*(y-a_real(2))) - (n5(3)*a_real(3)))./-(n5(3));
z_average = ((n_average(1).*(x-a_real(1))) + (n_average(2).*(y-a_real(2))) - (n_average(3)*a_real(3)))./-(n_average(3));
hold on
grid on; rotate3d on
plot3(v3x,v3y,v3z,'b','linew',2)
surf(x,y,z)
surf(x,y,z3)
surf(x,y,z4)
surf(x,y,z5)
surf(x,y,z_average)
nv = n_t.*n_t;
dp = sum(nv);
mag = sqrt(dp);
n3v = n3.*n3;
dp3 = sum(n3v);
mag3 = sqrt(dp3);
n4v = n4.*n4;
dp4 = sum(n4v);
mag4 = sqrt(dp4);
n5v = n5.*n5;
dp5 = sum(n5v);
mag5 = sqrt(dp5);
n_averagev = n_average.*n_average;
dp_average = sum(n_averagev);
mag_average = sqrt(dp_average);
theta2 = acosd((dot(n_t,n3v))/(mag*mag3));
theta3 = acosd((dot(n_t,n4v))/(mag*mag4));
theta4 = acosd((dot(n_t,n5v))/(mag*mag5));
theta_average(end+1,1) = acosd((dot(n_t,n_averagev))/(mag*mag_average));
k = k + 1 ;
if k > randi(5,1)
break
end
disp(k)
disp(theta_average)
writematrix(theta_average,filename,'Sheet',1,'Range','B2:B10')
end

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by