Question: I need help with the following MATLAB code. I want to pull out 8 matrices from the nested for loop. I want four matrices for

I need help with the following MATLAB code. I want to pull out 8 matrices from the nested for loop. I want four matrices for each noise level for guassian and possion distribution defined to a variable. How do I do that?
% Number of unit vectors
n =10;
% Define noise levels in degrees
noise_levels_deg =[1/3600,10/3600,1,5]; % arcseconds and degrees
% Define a random rotation matrix (Direction Cosine Matrix)
R = orth(randn(3)); %3x3 orthogonal matrix
% Generate 10 random unit vectors in the body frame
unit_vectors_body = randn(3, n);
unit_vectors_body = unit_vectors_body ./ vecnorm(unit_vectors_body);
% Define noise distributions (Gaussian and Poisson)
for noise_type =["Gaussian", "Poisson"]
disp(['Noise Type: ', char(noise_type)]);
for i =1:length(noise_levels_deg)
noise_level = noise_levels_deg(i);
% Convert noise level to radians
noise_level_rad = deg2rad(noise_level);
disp(['Noise Level: ', num2str(noise_level),' degrees']);
% Initialize arrays to hold noisy vectors
noisy_vectors_body = zeros(3, n);
noisy_vectors_inertial = zeros(3, n);
for j =1:n
% Apply noise to the unit vectors
if noise_type == "Gaussian"
noise = noise_level_rad * randn(3,1); % Gaussian noise
else
noise = noise_level_rad * poissrnd(1,3,1); % Poisson noise
end
% Perturb the unit vector's direction
perturbed_vector = unit_vectors_body(:, j)+ noise;
perturbed_vector = perturbed_vector / norm(perturbed_vector); % Renormalize
% Store noisy vector in body frame
noisy_vectors_body(:, j)= perturbed_vector;
% Transform to inertial frame using rotation matrix
noisy_vectors_inertial(:, j)= R * perturbed_vector;
end
% Display the noisy vectors in the inertial frame for this noise level
disp('Noisy Vectors in Inertial Frame:');
disp(noisy_vectors_inertial);
end
end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!