How to give a vector as Sigma to fspecial 'gaussian'

조회 수: 4 (최근 30일)
Kiarash Ahi
Kiarash Ahi 2018년 12월 22일
댓글: Walter Roberson 2018년 12월 23일
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

답변 (1개)

Walter Roberson
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
Kiarash Ahi
Kiarash Ahi 2018년 12월 23일
편집: Kiarash Ahi 2018년 12월 23일
Thank you for the quick response. Do you mean I should write it this way?
for i=1:60
for k=1:90
for j=1:40
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
Walter Roberson
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 CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by