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 Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by