How to generate fx value according to number of samples?

2 次查看(过去 30 天)
I have a matlab code of matrix multiplication with normal distribution
I need a output corresponding to number of samples = 20
I have a thickness data by normal distribution with 10x20 matrix . That number of rows data i used to generate matrix multiplication. I need fx values corresponds with that number of columns data( i.e, i need 20 output values)
How to do this in matlab.
clc
clear
close all
x=pi:0.01:2*pi; %%% x domain
mu=25;
sigma=1.5;
n=10;
num_samples=20;
d0=25; %%% thickness of layer 1
d1= normrnd(mu,sigma,[n,num_samples]); %%% thickness of layer 2 by normal distribution
for i =1:length(x)
x_value=x(i);
X1=x_value*d0;
Fx=[sin(X1) 1i; -1i cos(X1)]; %%% Matrix for layer 1
Fy = eye(2); % Initialize total matrix as identity for layer 2
for j = 1:n
X2=x_value*d1(j);
M=[sin(X2) 1i; -1i cos(X2)]; %%% Matrix for layer 1
Fy= Fy * M;
end
Fxx=(Fx^n*Fy); %%% Matrix multiplication
fx=Fxx(1,1)+Fxx(2,2); %%% output
end
disp(fx)

采纳的回答

Ayush Aniket
Ayush Aniket 2024-6-11
Hi Gulzar,
It seems like you are trying to perform matrix multiplication for a layered medium simulation, where the thickness of each layer is determined by a normal distribution. You want to generate 20 output values corresponding to the number of samples in your normal distribution matrix ('d1'), for a given set of 'x' values.
However, your code calculates 'fx' for each value of 'x' in the loop but overwrites it in each iteration, and the final 'disp(fx)' only shows the last computed value of 'fx'.
To achieve 20 distinct output values corresponding to each column in your 'd1' matrix (each sample), you need to adjust your approach as shown below:
clc
clear
close all
x = pi:0.01:2*pi;
mu = 25;
sigma = 1.5;
n = 10;
num_samples = 20;
d0 = 25;
d1 = normrnd(mu, sigma, [n, num_samples]);
% Initialize an array to store the output values of fx for each sample
fx_values = zeros(1, num_samples);
% Loop over each sample
for sample = 1:num_samples
Fy = eye(2); % Initialize total matrix as identity for layer 2
for i = 1:length(x)
x_value = x(i);
X1 = x_value * d0;
Fx = [sin(X1) 1i; -1i cos(X1)];
% Reset Fy for each x value
Fy = eye(2);
for j = 1:n
X2 = x_value * d1(j, sample);
M = [sin(X2) 1i; -1i cos(X2)];
Fy = Fy * M; % Update Fy for each layer
end
Fxx = (Fx^n * Fy);
fx = Fxx(1,1) + Fxx(2,2);
end
% Store the last computed fx value for this sample
fx_values(sample) = fx;
end
disp(fx_values)
In the above code, 'fx_values' stores the final 'fx' value for each sample. This is necessary because you want 20 distinct outputs corresponding to your 'num_samples'.

更多回答(0 个)

类别

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

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by