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
Walter Roberson
2018년 1월 9일
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개)
KSSV
2018년 1월 9일
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
Walter Roberson
2018년 1월 9일
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.
KSSV
2018년 1월 9일
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.
Walter Roberson
2018년 1월 9일
편집: Walter Roberson
2018년 1월 9일
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!