How to give a vector as Sigma to fspecial 'gaussian'
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 23 Dec 2018
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 Comments
Walter Roberson
on 23 Dec 2018
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
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!