First time matlab user problem Matrix dimensions must agree?

Hi,
I'm a first time matlab user and I've run into a bit of a problem I keep getting an error. Here is my sequence of codes:
derivefilter(FREQSPEC,600);
handle =
1
y =
173.0011
ans =
173.0011
filtering=applyfilter(pic1,ans);
Error using .*
Matrix dimensions must agree.
Error in applyfilter (line 3)
filteredspec=imgspec.*filter;
Here is the script "applyfilter":
function y=applyfilter(img,filter)
imgspec=fftshift(fft2(img));
filteredspec=imgspec.*filter;
y=real(ifft2(fftshift(filteredspec)));
Any help would be appreciated thanks.

 Accepted Answer

What does derivefilter() return? You're not accepting any matrix for the filter that you can then pass in to applyfilter() instead of ans. You may need to have something like
myFilter = derivefilter(FREQSPEC,600);
filtering=applyfilter(pic1, myFilter);
but we can't tell until we know what's inside derivefilter().

6 Comments

Thanks for your response, here is the derivefilter script:
function y=derivefilter(freqspec,imsize)
maximum=max(freqspec);
normfreqspec=freqspec./maximum;
for i=1:imsize
for j=1:imsize
[theta,rho]=cart2pol(i-(imsize/2)-1,j-(imsize/2)-1);
if (rho <= imsize/2) && (rho > 0)
filter(i,j)=normfreqspec(round(rho));
else
filter(i,j)=0;
end
end
end
filter((imsize/2)+1,(imsize/2)+1)=1;
y=filter;
showscr(y)
Appologies for not writing it in the appropriate form as I am currently in a rush. Thanks again.
ans is the result of showscr(y) so that is a handle and definitely NOT what you want to pass into applyfilter(). What you want to pass in is y. So what happens if you use my code? You can replace myFilter with y if you want, but you don't have to.
After inputting your code I am still met with the same error unfortunately.
I think it may have something to do with pic1 which is 600x600x3. I need to make it simply 600x600 or 600x600 double, though I don't know how as I'm so new to matlab.
pic1 = rgb2gray(pic1); %convert to 600x600
but you also need to solve the other issues.
Thanks for your help, re-sizing the pic1 variable resolved the issue.

Sign in to comment.

More Answers (2)

imgspec and filter must have the same dimension to do a element-wise multiplications
size(imspec)
size(filter)
Jan
Jan on 23 Apr 2013
Edited: Jan on 23 Apr 2013
Your code would be easier to read, if you spend the time for reading the manuals for this forum and format the code properly.
It looks like:
imgspec .* filter
cannot work, because the array sizes do not match. This is surprising, because the value of filter seems to be a scalar.
Using ans explicitly is a bad idea, because this value is very volatile and debugging can change the value. filter is not a good choice also, because this is a name of an important built-in function.

2 Comments

Thanks for the response, how exactly would I go about resolving this situation?
The scripts I use are laid out by my supervisor for my project. So I have to adhere to his methodology.
@Azfar: Then it is the job of your supervisor to find the solution. We cannot guess how this should be fixed, because all we see is the failing code. To find a solution, one has to know the intention of the code, but wrong code cannot carry this kind of information.

Sign in to comment.

Categories

Find more on Function Creation 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!