How to add a scalar to the first element in each row of an array?
7 次查看(过去 30 天)
显示 更早的评论
I am trying to execute a code to convert an entire trajectory in the rotating frame back into an inertial frame. I am having syntax issues with my for loop. I can't figure out how to properly add the scalar MU_Earth_Moon to the first element in each row of the 1001 x 6 array, S. As isee it, the problem is either in: S(ii,:) = S(ii,:) + MU_Earth_Moon or the CR3BP_to_Inertial function.
----------------------------------------------------------------------------------------------------------------
S_end = S(end, :)'
%% Part-5: Convert the S/C trajectory back to Earth-centered Inertial frame.
S_sc = zeros(size(S, 1), 6);
for ii = 1:size(S, 1)
% Retrieve the state in CR3BP Rotating frame wrt the barycenter.
x = S(ii, :)';
% Shift the state to be Earth-centered.
S(ii,:) = S(ii,:) + MU_Earth_Moon;
% Convert the state from CR3BP Rotating to Inertial frame.
S_sc(ii,:) = CR3BP_to_Inertial(t, S(ii,:));
% Convert the Inertial state to dimensional units.
S_sc(ii, 1:3) = [ lstar*x(1) lstar*S(ii) lstar*S(ii) ];
S_sc(ii, 4:6) = [ vstar*S(ii) vstar*S(ii) vstar*S(ii) ];
end
-------------------------------------------------------------------------------------------------------------
function [S] = CR3BP_to_Inertial(t, s)
% Define rotation matrix from Rotating to Inertial.
A = [ cos(0) -sin(0) 0;
sin(0) cos(0) 0;
0 0 1];
B = [ -sin(0) -cos(0) 0;
cos(0) -sin(0) 0;
0 0 0];
R_rotating_inertial = [ A zeros(3,3);
B A];
% Perform the Inertial to Rotating transformation of the state.
S = R_rotating_inertial*s;
end
0 个评论
采纳的回答
KSSV
2022-8-23
A = rand(5,6) ; % random data
[m,n] = size(A) ;
iwant = zeros(m,n+1) ;
iwant(:,2:end) = A ;
% Calculate the first element to append
for i = 1:5
iwant(i,1) = rand ;
end
iwant
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Coordinate Transformations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!