# How to add a scalar to the first element in each row of an array?

6 views (last 30 days)
Mark Munson on 23 Aug 2022
Commented: Mark Munson on 24 Aug 2022
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

KSSV on 23 Aug 2022
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
iwant = 5×7
0.5034 0.7816 0.1107 0.4789 0.7370 0.6817 0.3271 0.1004 0.1115 0.6400 0.3455 0.0215 0.2600 0.5143 0.6018 0.1929 0.5543 0.7796 0.9418 0.5785 0.9093 0.2524 0.6792 0.5463 0.0528 0.6098 0.3401 0.1031 0.9545 0.9418 0.3381 0.5326 0.5618 0.2948 0.1437
Mark Munson on 24 Aug 2022
Thank you!

### Categories

Find more on Matrices and Arrays in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!