Pad a matrix with additional rows and concatenate another column

조회 수: 3 (최근 30일)
Bill Symolon
Bill Symolon 2018년 4월 29일
댓글: Bill Symolon 2018년 4월 30일
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.

채택된 답변

Aref Majdara
Aref Majdara 2018년 4월 29일
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
Bill Symolon
Bill Symolon 2018년 4월 30일
Aref, that's exactly what I was looking for. Thanks!
Bill Symolon
Bill Symolon 2018년 4월 30일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by