Pad a matrix with additional rows and concatenate another column

3 views (last 30 days)
Hello, all!
I've included a sample of the code I'm using to pad a matrix with additional rows, then concatenate another column to that matrix. I can get the code to work for the first row of the matrix, but I'm not sure how to scale it for all six rows. I've tried using a for-loop but then get an error that the dimensions are not consistent.
mu = 398600.4415;
test_data = [6778.14 0 20 60 90 60; 6778.14 0 20 60 90 60; 7178.14 0 40 60 90 60; ...
7178.14 0 40 60 90 60; 7578.17 0 60 60 90 60; 7578.17 0 60 60 90 60];
% Calculate time in minutes
C = unique(test_data(:,1));
for i = 1:length(C)
if C(i) == 6778.14
T(i) = (sqrt((4*pi.^2)/(mu) * C(i).^3))/60;
elseif C(i) == 7178.14
T(i) = (sqrt((4*pi.^2)/(mu) * C(i).^3))/60;
else
T(i) = (sqrt((4*pi.^2)/(mu) * C(i).^3))/60;
end
end
% Divide the orbital period into 6 equal time steps
T_step = zeros(length(T), 6);
T_step(1,:) = [0:T(1)/5:T(1)];
T_step(2,:) = [0:T(2)/5:T(2)];
T_step(3,:) = [0:T(3)/5:T(3)];
T_step = T_step';
[m n] = size(T_step);
temp = padarray(test_data(1,:), [m-1 0], 'replicate','pre');
test_data = [temp, T_step(:,1)]
In the end, if the value in the first column of test_data is 6778.14, I need to concatenate the first column of T_step. Likewise the second column of T_step for 7178.14 and the third column for 7578.14.
I hope this is clear enough but if you need more detail, please let me know. I'd appreciate any help you can provide.

Accepted Answer

Aref Majdara
Aref Majdara on 29 Apr 2018
Hi Bill, I'm trying to understand your question. So, for this specific test data that you have provided, what are the dimensions of the matrix that you expect to get at the end? Is it 36x7 ?
  3 Comments
Bill Symolon
Bill Symolon on 30 Apr 2018
Aref,
I hope I can impose on you one more time. I've scaled the code you provided to include my entire dataset, but I'm still missing something. Here's the full code I'm using:
%%Define a range of orbital parameters as initial conditions
mu = 398600.4415; % Stnd grav parameter
% Set Altitude
Alt = 400:400:1200;
% Convert Altitude to Semi-major Axis
Ree = 6378; % Equatorial radius of Earth in km
a = Alt + Ree;
% Set Eccentricity
e = 1.7E-16; % Near zero
% Set Inclination
i = 20:20:60;
% Set Right Ascension of Ascending Node
RAAN = 0:60:300;
% Set Argument of Perigee
omega = 0:60:180;
% Set True Anomaly
nu = 0:60:300;
%%Calculate the orbital period in minutes
C = unique(a);
for z = 1:length(C)
if C(z) == 6778.14
T(z) = (sqrt((4*pi.^2)/(mu) * C(z).^3))/60;
elseif C(z) == 7178.14
T(z) = (sqrt((4*pi.^2)/(mu) * C(z).^3))/60;
else
T(z) = (sqrt((4*pi.^2)/(mu) * C(z).^3))/60;
end
end
%%Divide the orbital period into 6 equal time steps
T_step = zeros(length(T), 6);
T_step(1,:) = [0:T(1)/5:T(1)];
T_step(2,:) = [0:T(2)/5:T(2)];
T_step(3,:) = [0:T(3)/5:T(3)];
T_step = T_step';
[m n] = size(T_step);
%%Use nested for-loops to build matrix
index = 1;
for j = 1:length(a)
for k = 1:length(i)
for l = 1:length(RAAN)
for o = 1:length(omega)
for p = 1:length(nu)
parameters(index,:) = [a(j) e i(k) RAAN(l) omega(o) nu(p)];
index = index + 1;
end
end
end
end
end
%%Add the time steps to the parameters matrix
R=length(parameters);
for index=1:R
temp = padarray(parameters(index,:), [m-1 0], 'replicate','pre');
if parameters(index,1) == 6778.14
temp = [temp, T_step(:,1)];
elseif parameters(index,1) == 7178.14
temp = [temp, T_step(:,2)];
elseif parameters(index,1) == 7578.17
temp = [temp, T_step(:,3)];
end
if(index == 1)
M = temp;
else
M = [M; temp];
end
end
parameters = M;
Everything is working great, except the code doesn't add the time steps as the 7th column. When it's all said and done, I should end up with a 7776x7 matrix. Currently, I'm getting 7776x6.
I'd appreciate your thoughts. Thanks!

Sign in to comment.

More Answers (0)

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!