# How to modify loop code for 3D data?

2 views (last 30 days)

Show older comments

For 2D data when sizes or dimensions of v1, v2, r are 116 227, I used following code to compute data:

ti = 1 * 116

dt = ti(2)-ti(1);

nt = 116;

f0 = 30; %t = TWT;

t0 = 2/f0;

[wa,t] = wvlt(f0, t0, nt, dt);

w = w.'; t0_i = floor(t0/dt);

time = ti;

w = repmat(w, 1, 227);

%% Angles

ang = [15 30 45];

TH = ang*pi/180;

%% Forward model

for i = 1:size(v1,2)

for k = 1:length(TH)

data{i,k} = fwmod_v(v1(:,i),v2(:,i),r1(:,i),TH(k),w,t0,dt);

end

end

Now I want to modify the cde for 3D data of size

v1 = 176 139 48 i.e. 176 numb of vertical layers, 139 is horizontal traces and 48 are vertical slice each has 176 layers and 139 traces.

v2 = 176 139 48

r = 176 139 48

For 3D case how I can modify the above code?

Function

function [syn] = fwmod_v(v1,v2,r,TH,w,t0,dt)

t0_i = floor(t0/dt);

Nl = length(v1);

trc = AKR(v1,2s,r,TH);

syn = conv2(w, trc);

syn = syn(1+t0_i:Nl+t0_i,:);

function Rpp = AKR(v1, v1, r, TH)

Nt = length(v1);

Nth = length(TH);

R = zeros(Nt,Nth);

sin2 = sin(TH).^2;

tan2 = tan(TH).^2;

for i = 1:Nt-1

dvp = v1(i+1) - v1(i);

dvs = v2(i+1) - v2(i);

drho = r(i+1) - r(i);

vpm = (v1(i+1) + v1(i))/2;

vsm = (v1(i+1) + v1(i))/2;

rhom = (r(i+1) + r(i))/2;

R(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

##### 2 Comments

Daniel
on 5 Mar 2024

### Answers (1)

Daniel
on 5 Mar 2024

Based on what I can see, it looks like you're performing processing along one column of input data at a time. To amend that for 3D v1 and v2, you would just add a second layer to your for loop, so that you're iterating along both dimension 2 and dimension 3.

So you could add a for j = 1:size(v1,3) after your for i = ... line, and add an extra dimension to data and an additional layer of indexing to v1 and v2, such that that line becomes

data{i,j,k} = fwmod_v(v1(:,i,j),v2(:,i,j),r1(:,i,j),TH(k),w,t0,dt);

Does that work syntactically and do what you need?

##### 2 Comments

Daniel
on 5 Mar 2024

Edited: Daniel
on 5 Mar 2024

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!