coursera MATLAB course, blur image assignment

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 件のコメント

Geoff Hayes
Geoff Hayes 2020 年 10 月 28 日
Sara - should
for ((1<=(i + h)<=dim1) && (1<=(j+v)<=dim2));
be an if instead given that you have two conditions? What are you intending to do with this line of code?
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 10 月 28 日
to ensure the averages are taken within valid cells
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 10 月 28 日
thanks for your reply okay i've done that but now it doesn't appear to be iterating over the for loops as I intended, it is only yileding a [1,1] matrix so only calculating one cell I think?
Carolina Gaudenzi
Carolina Gaudenzi 2020 年 10 月 29 日
編集済み: Carolina Gaudenzi 2020 年 10 月 29 日
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 2020 年 10 月 29 日
yes i was getting this for some problems ! but not all of them. clicking 'submit' to de-bug seems to work better though !

サインインしてコメントする。

回答 (1 件)

Rik
Rik 2020 年 10 月 29 日

1 投票

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 件のコメント

Rik
Rik 2020 年 10 月 30 日
If this answer helped you solve the problem, please consider marking it as accepted answer. If not, feel free to post a comment with your remaining issues.
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 10 月 30 日
ok many thanks, I understnad 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,
poor mistake so I have changed that.
I am still stuck on what statement I should have instead or need in addition to get a proper answer instead of just a [1,1]. You say the for loops are not necessary so my thought was that for loops will keep runnign as long as the if condition is satisfied? but obviously this is not the case if it's just yielding a [1,1], which is why I wasn't sure whether I hsould have had a while or for loop there instead. thanks
Rik
Rik 2020 年 10 月 31 日
What is the code you have now? And did you step through your code line by line to see the result?
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 11 月 1 日
the same but the for loop replaced with an if statement. no i get messed up when i'm trying that
Rik
Rik 2020 年 11 月 2 日
Pre-allocate the output variable before the loop. Then you can write the result of your calculation in your loop to the output instead of the input.
I wrote a lot of things around your code. How did you try to incorporate those? For example: did you already write the documentation? Writing that first is sometimes a good idea.
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 11 月 2 日
sorry yesI did, inc renaming the variable ofc, this is what I have so far. what do you mean by write the documentation?
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;
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.
C(i,j)=(sum(A(i+h,j+v))\(2*w+1)^2);
end
end
end
end
D(i,j)=uint8(C(i,j))
output=D(i,j);
end
Rik
Rik 2020 年 11 月 2 日
Do you see the comments between the line with function and the first line of code? You should be writing that. Explain what you function does. Your user should not have to read your code to understand what your function is doing.
All other comment should be dealt with. They are remarks from me to you. Deal with them.
Why did you replace this
output=A(i,j);
% ^^^^^^
% you only retrieve a single value
with this?
D(i,j)=uint8(C(i,j))
output=D(i,j);
You still only use a single value to create your ouput.
You also only followed half of my advice: you didn't preallocate your output. Preallocation looks like this:
function out=twice(in)
%This function doubles the input.
%
% This function returns an array the same size as the input, but with each value double
% the original. There is no check for overflows, so uint8(200) will return uint8(255).
out=zeros(size(in));
for n=1:numel(in)
out(n)=in(n)*2;
end
end
Rik
Rik 2020 年 11 月 2 日
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 2020 年 11 月 2 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2020b

質問済み:

2020 年 10 月 28 日

コメント済み:

Rik
2020 年 11 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by