Clear Filters
Clear Filters

Rewrite the code be using vectorization instead of for loops and if statements.

3 views (last 30 days)
Rewrite the code be using vectorization instead of for loops and if statements. My code is below:
% Fetch TLE data from the URL
url_recent = 'https://celestrak.org/NORAD/elements/gp.php?GROUP=visual&FORMAT=tle';
line_list = string(splitlines(webread(url_recent)));
% Define Constant used in function
earth_mean_radius = 6371; % km
% Define the functions: launch_yr_4d(y2d) , apogee(mean_motion, eccentricity), perigee(mean_motion, eccentricity
launch_yr_4d = @(y2d) (y2d >= 24) .* (1900 + y2d) + (y2d < 24) .* (2000 + y2d);
apogee = @(mean_motion, eccentricity) (8681663.653 / mean_motion) .^ (2/3) .* (1 + eccentricity) - earth_mean_radius;
perigee = @(mean_motion, eccentricity) (8681663.653 / mean_motion) .^ (2/3) .* (1 - eccentricity) - earth_mean_radius;
% Loop through each line of the TLE data
for i = 1:3:numel(line_list)
% Ensure there are enough lines left in line_list
if numel(line_list) < i+2
break;
end
% Extract catalog number, TLE parameters, and launch year
catalog_number = char(line_list(i));
tle_line1 = char(line_list(i+1));
tle_line2 = char(line_list(i+2));
% Extract satellite number from the first line of TLE data
satellite_number_str = tle_line1(3:7);
satellite_number = str2double(satellite_number_str);
% Ensure satellite number is represented as a 5-digit number
satellite_number_str = sprintf('%05d', satellite_number);
% Extract launch year from the first line of TLE data
launch_year_2digit = str2double(tle_line1(10:11));
launch_year = launch_yr_4d(launch_year_2digit);
% Extract eccentricity (e0) and mean motion (n0) from the second line of TLE data
eccentricity = str2double(tle_line2(27:33)) * 0.0000001;
mean_motion = str2double(tle_line2(53:63));
% Compute perigee and apogee heights using the defined functions
hp = perigee(mean_motion, eccentricity);
ha = apogee(mean_motion, eccentricity);
% Print the results in one line and round perigee and apogee
fprintf('%s %04d %.2f %.2f %s\n', satellite_number_str, launch_year, round(hp, 2), round(ha, 2), catalog_number);
end

Answers (2)

Aditya
Aditya on 21 Mar 2024
Hi Bob,
I understand that you want to vectorize the code that processes 'TLE' (Two-Line Element) data for satellites to enhance performance by eliminating 'for loops' and 'if statements'. Vectorization in MATLAB allows you to work with batches of data at once, leveraging MATLAB's optimized array operations for efficiency.
You can vectorize the code by first reshaping your line list into a structured format where each column represents a single satellite's TLE data. Then, apply the calculations to these batches of data using MATLAB's array operations. Here's how you can do it:
% Fetch TLE data from the URL
url_recent = 'https://celestrak.org/NORAD/elements/gp.php?GROUP=visual&FORMAT=tle';
line_list = string(splitlines(webread(url_recent)));
% Ensure line_list has a multiple of 3 number of lines for proper reshaping
line_list = line_list(1:floor(numel(line_list)/3)*3);
% Reshape the line list to separate each TLE into its own column
tle_data = reshape(line_list, 3, []);
% Define Constant used in function
earth_mean_radius = 6371; % km
% Define the functions: launch_yr_4d(y2d), apogee(mean_motion, eccentricity), perigee(mean_motion, eccentricity)
launch_yr_4d = @(y2d) (y2d >= 24) .* (1900 + y2d) + (y2d < 24) .* (2000 + y2d);
apogee = @(mean_motion, eccentricity) (8681663.653 ./ mean_motion) .^ (2/3) .* (1 + eccentricity) - earth_mean_radius;
perigee = @(mean_motion, eccentricity) (8681663.653 ./ mean_motion) .^ (2/3) .* (1 - eccentricity) - earth_mean_radius;
% Extract satellite numbers, launch years, eccentricities, and mean motions in batches
satellite_numbers = str2double(extractBetween(tle_data(2,:), 3, 7));
launch_years_2digit = str2double(extractBetween(tle_data(2,:), 10, 11));
launch_years = launch_yr_4d(launch_years_2digit);
eccentricities = str2double(extractBetween(tle_data(3,:), 27, 33)) * 0.0000001;
mean_motions = str2double(extractBetween(tle_data(3,:), 53, 63));
% Compute perigee and apogee heights in batches
hps = perigee(mean_motions, eccentricities);
has = apogee(mean_motions, eccentricities);
% Print the results
for i = 1:size(tle_data, 2)
fprintf('%05d %04d %.2f %.2f %s\n', satellite_numbers(i), launch_years(i), round(hps(i), 2), round(has(i), 2), tle_data(1, i));
end
This approach eliminates the need for looping through each line individually and conditionally checking if there are enough lines left. Instead, it processes all the TLE data in batches, significantly improving the efficiency of your code, especially with large datasets.
Hope this helps!
  2 Comments
Bob Meyes
Bob Meyes on 21 Mar 2024
Thank you for the answer!
Is there any wat to eliminate the last for loop in the code (the printing results part)
Aditya
Aditya on 21 Mar 2024
To eliminate the final loop for printing, you can concatenate and format the strings using MATLAB's vectorized operations as follows:
% Convert numerical values to strings with formatting
satNumStr = sprintfc('%05d', satellite_numbers);
launchYearStr = sprintfc('%04d', launch_years);
hpStr = sprintfc('%.2f', round(hps, 2));
haStr = sprintfc('%.2f', round(has, 2));
% Concatenate all string parts
outputStrings = strcat(satNumStr, " ", launchYearStr, " ", hpStr, " ", haStr, " ", string(tle_data(1, :)));
% Join and print all strings
fprintf('%s\n', join(outputStrings, newline));
This approach formats and concatenates the strings without explicit looping, then prints all formatted lines at once.

Sign in to comment.


Dinesh
Dinesh on 21 Mar 2024
Hello Bob.
To vectorize the provided MATLAB code, start by reshaping the TLE data into a 2D matrix for batch processing. Utilize MATLAB's vectorized string functions to extract necessary parameters across all data simultaneously. Apply array operations for calculations like launch year conversion, and apogee and perigee computations, ensuring all mathematical operations are adapted for element-wise processing. Finally, consolidate output formatting and printing into a vectorized or batch operation as much as possible, although some iteration may be necessary for displaying results.
Here's a link to the documentation that can help you:

Community Treasure Hunt

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

Start Hunting!