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

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

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

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 then how to read each set separately as 116x3?
Another thing it should be 116x3 rather than 116x227 as in the image
Change the outer for loop to row dimension as below
theta = [15 30 45];
for i = 1:116 % size(vp,1) % change to row dimension
for k = 1:length(theta)
data{i,k} = fwmod_vpvsrho(vp(i,:),vs(i,:),rho(i,:),theta(k),wav,t0,dt)
end
end
to read each set separately,
data{1,1}(:) % access all elements of first set for first value of theta ... extend same to others
@VBBV It is not working e.g., now data size is 116 * 3 and all data cells have zero values.
It should be 116 rows and 227 columns and for each column there must be three columns (for three theta values)
theta = [15 30 45];
for i = 1:227 % size(vp,1) % change to row dimension
for k = 1:116
data{i,k} = fwmod_vpvsrho(vp(k,i),vs(k,i),rho(k,i),theta,wav,t0,dt)
end
end
I dont understand what you mean by for each column there must be three columns (for three theta values)
Ok. Then try as above. Also, to access the individual data sets inside the cell array, use the indexing as mentioned earlier.
@VBBV Following copied below is working: but with index below I am extracting first trace from all data{1,1}; data{1,2}; data{1,3};
If I want to extract all ntrc, mtrc and ftrc to make three sets e.g. ntrc = 116 * 227, mtrc = 116 * 227, ftrc = 116 * 227
Cai I do like this
DATA1 = data{1,1}; ntrc = DATA1(:,:);
Code is
%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:size(vp,2)
for k = 1:length(theta)
data{i,k} = fwmod_vpvsrho(vp(:,i),vs(:,i),rho(:,1),theta(k),wav,t0,dt);
end
end
DATA1 = data{1,1}; ntrc = DATA1(:,1);
DATA2 = data{1,2}; mtrc = DATA2(:,1);
DATA3 = data{1,3}; ftrc = DATA3(:,1);
gather = [ntrc,mtrc,ftrc];
figure, wiggle(gather,time,theta);
xlabel('Angle (deg)'); ylabel('Time (ms)'); ylim([2 2.2]); set(gca,'ytick',[2.0:0.05:2.4]); xlim([-0.0 1.0]); set(gca, 'fontsize', 11,'fontweight','normal');
set(gcf, 'position', [600 285 285 550]); title('S/N = 20','fontweight','normal');
%
Yes, you can do it but doing it for 227 times you can modify the code slighly, as below so that all datasets for gather becomes 116 x 3
% after this
for i = 1: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
% do this for the extracted data
for K = 1:227
DATA1 = data{K,1};
DATA2 = data{K,2};
DATA3 = data{K,3};
ntrc = DATA1(:,K);
mtrc = DATA2(:,K);
ftrc = DATA3(:,K);
gather{K} = [ntrc mtrc ftrc]; % this should be 116 x 3
end
size(gather{1}) % gather now contains datasets of size 116 x 3
% you can call the function wiggle inside the above for loop to access the
% individual datasets of size 116 x 3 one at a time
@VBBV Thank you very much and really appreciated.
@VBBV I have a one more question on above problem,
If I want add noise on each gather e.g., gather(1), I can add following way:?
seis = var(gather(1)); SNR = 20; % add noise
var_noise = var(gather(1))./SNR;
std_noise = sqrt(seis./SNR);
Noise_data = data + std_noise.*randn(size(data));
gather(1) = Noise_data;
But If I want add error/noise on all the gathers - gather(K) together. Then I can modify it as below or I need to apply loop here? var - varrience for each gather should be based on each (same) gather and so on.. Can you help how to modify following code?
seis = var(gather(K)); SNR = 20; % add noise
var_noise = var(gather(K))./SNR;
std_noise = sqrt(seis./SNR);
Noise_data = data + std_noise.*randn(size(data));
gather(K) = Noise_data;
Where I am making mistake in the following code to extract the first column of each matrix in gather{K} to create new matrices Snear, Smid and Sfar?
% Assuming gather(K) is a cell array with matrices of size 116x3 for each K
K = 1:227;
gatheredData = cell(116, 3, 227);
% The code to populate gatheredData goes here...
% Initialize matrices Snear, Smid, and Sfar
Snear = zeros(116, 227);
Smid = zeros(116, 227);
Sfar = zeros(116, 227);
% Extract the first column from each matrix and populate Snear, Smid, and Sfar
for i = 1:227
% Assuming gather(K) is a cell array
currentMatrix = gather(K(i));
% Extract the first column
Snear(:, i) = currentMatrix(:, 1);
Smid(:, i) = currentMatrix(:, 2);
Sfar(:, i) = currentMatrix(:, 3);
end
Error in M_2D_Forward (line 60)
Snear(:, i) = currentMatrix(:, 1);
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
@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)

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

@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.

Asked:

on 3 Feb 2024

Edited:

on 6 Feb 2024

Community Treasure Hunt

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

Start Hunting!