How to give a vector as Sigma to fspecial 'gaussian'
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I need to give a vector to fspecial as the Sigma of a Gaussian. If I want to use a for loop inside another for loops and hence it will become imposible to handle the task that I need to do. I was wondering if it is posible to give a vector to fspecial 'gaussian'? When I give a vector, It gives me this error: "Error using fspecial Expected input number 3, SIGMA, to be a scalar."
Current script (works, but very slow due to three "for loops"):
for j=1:40
for k=1:90
for i=1:60
x=(fft(sp(35:65,j,k)));
x=abs(x(:,1));
PSF_i(:,:,i,j,k)=x(i,1)).*(fspecial('gaussian', 111, floor(y(i,1))));
end
end
end
Desired Script: ( It gives me this error: "Error using fspecial Expected input number 3, SIGMA, to be a scalar.")
for j=1:40
for k=1:90
x=(fft(sp(35:65,j,k)));
x=abs(x(:,1));
PSF_i(:,:,:,j,k)=x(:,1)).*(fspecial('gaussian', 111, floor(y(:,1))));
end
end
0 个评论
回答(1 个)
Walter Roberson
2018-12-23
No. However you can call it less often.
Change the order of your for loops so that the outer loop is for i. As the first thing inside the loop call fspecial passing in y indexed by i and assign the result to a variable . Then use the variable inside the inner loop.
2 个评论
Walter Roberson
2018-12-23
for i=1:60
fsp = fspecial('gaussian', 111, floor(y(i,1)));
for k = 1:90
fftk = abs(fft(sp(35:65,:,k));
for j=1:40
x = fftk(:,j);
PSF_i(:,:,i,j,k) = x(i,1) .* fsp;
end
end
end
Note that this will encounter the same index out of range error that your original code had. Look closely at your original code:
for j=1:40
for k=1:90
for i=1:60
x=(fft(sp(35:65,j,k)));
%35:65 is 31 elements, so sp (35:65,j,k) has 31 rows
%and is a column vector. fft() of a column vector is
%the same size as the input, so x will now be a column vector
%of length 31
x=abs(x(:,1));
%asking for the first column of a column vector is the
%entire column vector so the (:,1) is redundant but valid
%abs() of a column vector is the same length as the
%column vector so x is now a column vector of length 31
PSF_i(:,:,i,j,k)=x(i,1)).*(fspecial('gaussian', 111, floor(y(i,1))));
%asking for the single i'th row and first column of a column
%vector of length 31 is valid up to i = 31. But your for loop
%goes to i = 60, so you get an index out of range.
%Or you WOULD get an index out of range, if your code had valid
%syntax, which it does not because of the unmatched ) right
%after x(i,1)
%note that x(i,1) will be a scalar, so you are asking for
%a scalar times the 111 x 111 gaussian
end
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!