How to modify the code to compute data on multiple inputs?

3 views (last 30 days)
Hi,
I have attached the snap of my code, I am computing data by using vp vs and rho each of size 116 * 227. The data should be computed as e.g., data1 from vp(:,1), vs(:,1) and rho(:,1) on three theta (15 30 45) and then in the next stage: data2 from vp(:,2), vs(:,2) and rho(:,2) on three theta (15 30 45) and so on... until 227.
How can I adjust my code to perform like this? It means I should have 227 set of output data each have three traces at three theta values (as theta are 15 30 45).
  1 Comment
Jo
Jo on 4 Feb 2024
Certainly! Here's a shorter version:
  1. Function Signature:Update the function signature to accept multiple inputs.python
  • def your_function(*inputs):
# existing code
  • Loop or Iterate:If inputs are iterable, loop through them.python
  • def process_multiple_inputs(inputs):
for input_data in inputs:
# process input_data
  • Parallel Processing:For parallel processing, consider using concurrent.futures.python
  • from concurrent.futures import ThreadPoolExecutor
def process_inputs_parallel(inputs):
with ThreadPoolExecutor() as executor:
results = list(executor.map(process_function, inputs))
  • Modularization:Break code into smaller functions for readability.python
  1. def process_input(input_data):
# process individual input
def process_multiple_inputs(inputs):
results = [process_input(data) for data in inputs]
Adjust the suggestions based on your specific code and requirements.

Sign in to comment.

Accepted Answer

VBBV
VBBV on 4 Feb 2024
Edited: VBBV on 4 Feb 2024
theta = [15 30 45];
for i = 1:227 % size(vp,2)
for k = 1:length(theta)
data{i,k} = fwmod_vpvsrho(vp(:,i),vs(:,i),rho(:,i),theta(k),wav,t0,dt)
end
end
An alternate way is to use double for loop which considers the 227 output values in vector for each theta
  10 Comments
VBBV
VBBV on 5 Feb 2024
Edited: VBBV on 5 Feb 2024
You can modify the code as below. Note that gather is cell array created for datasets of size 116 x 3. So you must use a curly brace like { } instead of parenthesis ( )
SNR = 20; % add noise
for K = 1:227
% Assuming gather(K) is a cell array
currentMatrix = gather{K};
% Extract the first column
Snear(:, K) = currentMatrix(:, 1); % first theta
Smid(:, K) = currentMatrix(:, 2); % second theta
Sfar(:, K) = currentMatrix(:, 3); % third theta
% Adding Noise
seis = var(gather{K});
var_noise = var(gather{K})./SNR;
std_noise = sqrt(seis./SNR);
Noise_data1 = data{K,1} + std_noise.*randn(size(data{K,1})); % first theta
Noise_data2 = data{K,2} + std_noise.*randn(size(data{K,2})); % second theta
Noise_data3 = data{K,3} + std_noise.*randn(size(data{K,3})); % third theta
Noise_gather{K} = [Noise_data1 Noise_data2 Noise_data3] ;
end
Regarding the adding of noise data, the code can be modified as above. Also note that, you aleady have variable named gather so use a different variable name for gathering noise data e.g. Noise_gather
Ahmed
Ahmed on 6 Feb 2024
Edited: Ahmed on 6 Feb 2024
@VBBV Thanks and there is an error here:
Because size of std_noise is 1 3 and size of randn(size(data{K,1})) = 116 227.
Matrix dimensions must agree.
Error in M_2D_Forward (line 63)
Noise_data1 = data{K,1} + std_noise.*randn(size(data{K,1})); % first theta

Sign in to comment.

More Answers (1)

Aquatris
Aquatris on 3 Feb 2024
Edited: Aquatris on 3 Feb 2024
Next time please copy paste the code, not screen shot :D
You did not provide full info to get an answer since I have no idea how fwmod_vpvsrho function you have.
Having said that you can either modify your fwmod_vpvsrho function to allow an array inputs for theta variable or you can do:
for i = 1:length(theta)
data(:,:,i) = fwmod_vpvsrho(vp,vs,rho,theta(i),wav,t0,dt)
end
WIth this data(:,:,i) refers to the data at theta(i). so the data variable is a 116x227x3 matrix.
  1 Comment
Ahmed
Ahmed on 4 Feb 2024
@Aquatris Thaks, but the oupt this way is 227x227x3 instead of 116x227x3?
This is code given below. How can I read all 227 datasets separately as data1 = 116x3?
load('Modell.mat','vp','vs','rho')
rho = rho.*1000;
vs(vs==0) = 100;
ip = vp.*rho; is = vs.*rho;
load('time.mat','ti');
Travel_min = 2.0; Travel_max = 2.2;
ti = linspace(Travel_min,Travel_max,116)';
ti = ti';
%% Wavelet
dt = ti(2)-ti(1);
nt = 116;
f0 = 30; %t = TWT;
t0 = 2/f0;
[wav,t] = Ricker(f0, t0, nt, dt);
wav = wav.'; t0_i = floor(t0/dt);
time = ti; %figure, plot(wav)
wav = repmat(wav, 1, 227);
%% Angles
angles = [15 30 45];
theta = angles*pi/180;
%% Forward model
for i = 1:length(theta)
data(:,:,i) = fwmod_vpvsrho(vp,vs,rho,theta(i),wav,t0,dt);
end
% Functions
function [synth] = fwmod_vpvsrho(vp,vs,rho,theta,wav,t0,dt)
t0_i = floor(t0/dt);
Nl = length(vp);
trc = Aki_Richards(vp,vs,rho,theta);
synth = conv2(wav, trc);
synth = synth(1+t0_i:Nl+t0_i,:);
function Rpp = Aki_Richards(vp, vs, rho, theta)
Nt = length(vp);
Ntheta = length(theta);
Rpp = zeros(Nt,Ntheta);
sin2 = sin(theta).^2;
tan2 = tan(theta).^2;
for i = 1:Nt-1
dvp = vp(i+1) - vp(i);
dvs = vs(i+1) - vs(i);
drho = rho(i+1) - rho(i);
vpm = (vp(i+1) + vp(i))/2;
vsm = (vs(i+1) + vs(i))/2;
rhom = (rho(i+1) + rho(i))/2;
Rpp(i,:) = 0.5*(1 + (tan2)).*(dvp/vpm) - 4*(((vsm/vpm)^2)*dvs/vsm).*sin2 + 0.5*(1 -4*((vsm/vpm)^2).*sin2)*(drho/rhom);
end

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!