matlab code for python script?

1 次查看(过去 30 天)
i am trying to write a matlab code for python script as:
class structure:
def __init__(self, shape, eps, mu):
self.shape = shape
self.eps = eps
self.mu = mu
self.epsr = np.ones(shape, dtype='float64')
self.mur = np.ones(shape, dtype='float64')
class Sphere(structure):
def __init__(self, shape, center, R, eps, mu, smoothing = False):
super(Sphere, self).__init__(shape, eps, mu)
self.center = center
self.R = R
self.smoothing = smoothing
self.get_epsr()
def get_epsr(self):
x = np.arange(self.shape[0])
y = np.arange(self.shape[1])
z = np.arange(self.shape[2])
X,Y,Z = np.meshgrid(x,y,z)
self.r = np.sqrt((self.center[1]-X)**2 + (self.center[0]-Y)**2 + (self.center[2]-Z)**2)
self.region = np.where(self.r >= self.R, 0, 1)
print("self.region value: ",self.region)
print("self.region shape: ",self.region.shape)
self.epsr += self.region * (self.eps - 1)
if self.smoothing == True:
smoothing_region = 1 - (np.where((0 < (self.r - self.R)) & ((self.r - self.R) < 1), 1/3*(self.r+np.sqrt(self.r**2-1)-2*self.R)**2, 1))
self.epsr += smoothing_region * (self.eps - 1)
del self.r
the call for these structures in python is as:
um = 1e-6
dx = 4.8 * um
lamb = 74.9*um
eps = np.zeros((250,250))
str1 = Sphere(shape = (250,250,250), center = (125,125,125), R = lamb*3/dx, eps=10, mu=1)
THE MATLAB SCRIPT WHICH I TRIED IS AS:
shape = [250,250,250]
N1=shape(1);
N2=shape(2);
N3=shape(3);
um = 1e-6
c = 3e8 % m/s
dx = 4.8 * um
dy = 4.8 * um
dz = 4.8 * um
dt = 1/4 * dx / c
lamb = 74.9*um
R = lamb*3/dx;
eps=10;
mu=1;
x = linspace(0,N1-1,N1);
y = linspace(0,N2-1,N2);
z = linspace(0,N3-1,N3);
[X,Y,Z] = meshgrid(x,y,z);
X=(X(:))';
Y=(Y(:))';
Z=(Z(:))';
center = [125, 125,125];
r = sqrt((center(2)-X).^2 + (center(1)-Y).^2 + (center(3)-Z).^2);
epsr = ones(shape);
mur = ones(shape);
for k=1:1:length(r)
for j = 1:1:length(r)
for k=1:1:length(r)
if r(:,:,k) >= R
epsr(:,:,k) =
Q: i am struck with implementation of region in matlab, can someone help me with this

采纳的回答

Paras Gupta
Paras Gupta 2023-9-22
Hi Asim,
I understand that you want to convert the given python code to MATLAB.
In the given Python code, the region variable is calculated using np.where(self.r >= self.R, 0, 1). This condition sets the value of region to 0 where self.r >= self.R and 1 otherwise.
To implement this in MATLAB, you can use logical indexing as shown below:
shape = [250,250,250];
N1=shape(1);
N2=shape(2);
N3=shape(3);
um = 1e-6;
c = 3e8; % m/s
dx = 4.8 * um;
dy = 4.8 * um;
dz = 4.8 * um;
dt = 1/4 * dx / c;
lamb = 74.9*um;
R = lamb*3/dx;
eps=10;
mu=1;
x = linspace(0,N1-1,N1);
y = linspace(0,N2-1,N2);
z = linspace(0,N3-1,N3);
[X,Y,Z] = meshgrid(x,y,z);
% X=(X(:))';
% Y=(Y(:))';
% Z=(Z(:))';
center = [125, 125,125];
r = sqrt((center(2)-X).^2 + (center(1)-Y).^2 + (center(3)-Z).^2);
epsr = ones(shape);
mur = ones(shape);
region = zeros(size(r));
region(r >= R) = 0;
region(r < R) = 1;
epsr = ones(shape);
epsr = epsr + region * (eps - 1);
smoothing = false;
% Calculate smoothing_region
smoothing_region = zeros(size(r));
if smoothing==true
temp = (0 < (r - R)) & ((r - R) < 1);
smoothing_region(temp) = 1/3 * (r(temp) + sqrt(r(temp).^2 - 1) - 2 * R).^2;
end
% Update epsr with smoothing_region
epsr = epsr + smoothing_region * (eps - 1);
You can refer to the following documentation on array indexing for more information on the use of logical indexing.
Hope this helps.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by