How to write a function that produces a 4D array?

Hi, I am struggling to write a function for this equation with a delta function? How should I go about doing this? Thank you

1 个评论

To confirm, the function is normally to be 0, but in the case where t exactly equals r/c, that the value is to equal p0/(4*pi*r) ?
If so all entries are to be 0 except if there happens to be a t corresponding exactly to r/c (bit-for-bit identical, so floating point round-off is very important), and only that one hyperplane is populated with values that are constant divided by the euclidean distance to a particular point (xs, ys, zs).

请先登录,再进行评论。

回答(1 个)

N = 100 ;
% some random data to check
p0 = rand ; c = rand ;
x = rand(N,1) ;
y = rand(N,1) ;
z = rand(N,1) ;
t = linspace(0,2*pi,N)' ;
xs = 0.; ys = 0.; zs = 0. ;
r = sqrt((x-xs).^2+(y-ys).^2+(z-zs).^2) ;
p = zeros(N,1) ;
tol = 10^-3 ;
idx = abs(t-r/c)<=tol ;
p(idx) = p0/(4*pi*r(idx)) ;

3 个评论

Your r is a vector but you use / to divide by it. That will only work if only one item is selected by idx.
You appear to be creating a vector, not a 4D array.
I used / to divide with r..because rest are constants.
I think p would be a vector. I don't think a 4D array is expected here.
The user asked for a 4D array result.
It would, I think, make the most sense if the inputs were arrays and the output were expected to be the same size. It would be understandable, though, if the user wished to input vectors and have an ndgrid of results generated. But with most of the outputs being zero... perhaps a sparse output ? Though that would require using one of the File Exchange contributions, since sparse arrays are normally restricted to vectors or 2D.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by