coursera MATLAB course, blur image assignment

20 views (last 30 days)
My code is as below, but I am getting the error message
The server timed out while running and assessing your solution. Use the Run button to verify your code. If successful, submit again.If the error persists, contact the instructor.
function[output]=blur(A,w)
[dim1,dim2]= size(A);
for i=1:w;
for j=1:w;
for h = 0:w;
for v=0:w;
for ((1<=(i + h)<=dim1) && (1<=(j+v)<=dim2));
A(i,j)=(sum(A(i+h,j+v))\(2*w+1)^2);
end
end
end
end
end
output=A(i,j);
end
On a seperate note, in line 12, I am not sure whetehr is should be a for condition, or a while condition? Can they be used interchangeably here or not?
Many thanks !
(these were the instructions:)
  5 Comments
Carolina Gaudenzi
Carolina Gaudenzi on 29 Oct 2020
Edited: Carolina Gaudenzi on 29 Oct 2020
I am trying to submit the "Excel Files" assignment, and I am getting the same error message (The server timed out while running and assessing your solution. Use the Run button to verify your code. If successful, submit again.If the error persists, contact the instructor.)
Are you still getting it as well? Is there anything wrong with the server?
Sara Ismail-Sutton
Sara Ismail-Sutton on 29 Oct 2020
yes i was getting this for some problems ! but not all of them. clicking 'submit' to de-bug seems to work better though !

Sign in to comment.

Answers (1)

Rik
Rik on 29 Oct 2020
Using the smart indent is generally a good idea. Now let's go through your code (ignoring the quadruple nested loop that can probably be reduced to fewer loops, drastically improving performance).
It looks like you need to modify the outer two loops, but due to the complete lack of comments, I am not sure.
function[output]=blur(A,w)
%one-liner descriptions goes here
%
%function documentation (input,output,syntax option) goes here
[dim1,dim2]= size(A);
for i=1:w;
% ^
% not required
for j=1:w;
% ^
% not required
for h = 0:w;
% ^
% not required
for v=0:w;
% ^
% not required
%as Geoff suggested: replace for by if:
if ((1<=(i + h)<=dim1) && (1<=(j+v)<=dim2));
% ^^ ^^
% This doesn't do what you think it does.
% Read what the linter is telling you.
A(i,j)=(sum(A(i+h,j+v))\(2*w+1)^2);
%^^^^^
% You're storing the output in the input variable.
% The problem is that you are overwriting values
% that you may need in a later iteration.
end
end
end
end
end
output=A(i,j);
% ^^^^^^
% you only retrieve a single value
end
  9 Comments
Rik
Rik on 2 Nov 2020
Comments posted as answer and 2 comments:
output=A(i,j);
% ^^^^^^
% you only retrieve a single value
what else should it be then? i thought it iterates over i and j so will get all the elements eventually. you wouldm't just write 'A' so I have no ideea what to do
re the comments i thought it would be pretty obvious for guys who know what theya re doing in matlab vs me who has just started out, adn yes, i dealt with the oens i could
D(i,j)=uint8(C(i,j))
output=D(i,j);
because i renamed in the loop because as you said i was over writing values i wanted to use in the iteration? C is in the loop and then I went to D because it is yileding the class as a double, a quick google said that code would convert it (though it does not seem to work), i want a uint8. but now I realise, from what you have said that it is because it is only giving a single value.
Rik
Rik on 2 Nov 2020
You correctly replaced the right side of the assignment inside your loop. That means the output is already stored in C. Since i and j are just scalars, why would you need to use them to index anything outside of the loop? You could have discovered this on your own if you had used the debugging tools to go through your code line by line.
Regarding the comments: writing the comments is not a useless excercise. You don't just write them for others, but also for yourself. If you look back at your code in a week, a month, or a year, will you understand what it did? Immediately? You may think the answer is yes, but it often isn't. Learn to write comments when your code is simple, so you have the habit (and the experience) when you code is complex.
There are still lines you can deal with: there are still 4 unnecessary semicolons. Just remove them and remove those comments. And read the advice mlint is giving you about your comparison in the if-statement. It contains the exact explanation of how you should re-write that expression. If you don't know how, feel free to post a comment here (but show that you attempted to read the mlint warning (hover your mouse over orange line)).
Regarding your last comment: and that is why you should start by writing the documentation. I didn't know you wanted a uint8. You never told anyone. Don't let me guess, and don't let your users guess. You should also think about what is happening inside your loop. You store a value to C(i,j). How often will that line be visited for any given value of i and j? Use the debugging tools.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!