Pad a matrix with additional rows and concatenate another column

2 次查看(过去 30 天)
Hello, all.
I asked this question a couple of days ago and you provided some excellent help. I've taken your code and scaled it up to my full data set, but it's still not working quite right. The goal is to end up with a matrix that is 7776x7.
The code runs through without errors, but the resulting matrix is 7776x6. The 7th column (time) isn't being concatenated properly. I hoping you can tell me what I'm missing. Here is my full code.
if true
% Define initial conditions
mu = 398600.4415;
Alt = 400:400:1200;
Ree = 6378;
a = Alt + Ree;
e = 1.7E-16;
i = 20:20:60;
RAAN = 0:60:300;
omega = 0:60:180;
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; % Period
elseif C(z) == 7178.14
T(z) = (sqrt((4*pi.^2)/(mu) * C(z).^3))/60; % Period
else
T(z) = (sqrt((4*pi.^2)/(mu) * C(z).^3))/60; % Period
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';
% Nested for-loops
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;
end
Thanks again for any help you can provide!

采纳的回答

Walter Roberson
Walter Roberson 2018-5-3
Those comparisons on the parameter values will probably never be true.
  2 个评论
Bill Symolon
Bill Symolon 2018-5-3
Thanks, Walter. I modified the code based on the FAQ and I'm still getting the same result. I'm sure it's operator error.
if true
% Add the time steps to the parameters matrix
[m n] = size(T_step);
R=length(parameters);
tol1 = eps(6778.14);
tol2 = eps(7178.14);
tol3 = eps(7578.14);
for index=1:R
temp = padarray(parameters(index,:), [m-1 0], 'replicate','pre');
if ismembertol(parameters(index,1), 6778.14, tol1)
temp = [temp, T_step(:,1)];
elseif ismembertol(parameters(index,1), 7178.14, tol2)
temp = [temp, T_step(:,2)];
elseif ismembertol(parameters(index,1), 7575.14, tol3)
temp = [temp, T_step(:,3)];
end
if(index == 1)
M = temp;
else
M = [M; temp];
end
end
parameters = M;
end
I tried two versions. "if ismembertol(parameters(index,1), 6778.14, tol1)" and "if ismembertol(parameters(index,1), tol1)"
They both ran without errors, but I'm still getting a 7776x6 matrix. I'd appreciate any other thoughts you might have.
Walter Roberson
Walter Roberson 2018-5-3
I suggest you debug with
vals = [6778.14, 7178.14, 7575.14];
for K = 1 : length(vals)
[~, idx] = min( abs(parameters(:,1)-vals(K)) );
fprintf('%closest to %g is at index %d and is %g which is a difference of %g\n', vals(K), idx, parameters(idx,1), parameters(idx,1)-vals(K));
end

请先登录,再进行评论。

更多回答(1 个)

Yuvaraj Venkataswamy
Check this line,
temp = padarray(parameters(index,:), [m-1 1], 'replicate','pre');
  1 个评论
Bill Symolon
Bill Symolon 2018-5-3
Yuvaraj,
My mistake. When I copied the code into the question field, I missed one line: [m n] = size(T_step);
I don't think the padarray function is the problem.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by