How to create a function that returns a matrix with one more popped piece randomly added and the location of it.

Hi there, I am stuck on a question and don't know how to go about it:
Write a function that given a matrix M of numbers representing locations and the number of popcorn pieces that have previously popped at each, returns a matrix with one more popped piece randomly added. In addition, the function returns the row-column coordinate where the latest piece popped.
For example
>> a = zeros(2)
a =
0 0
0 0
>> aa = popcorn(a)
aa =
1 0
0 0
>>[aaa rc] = popcorn(aa)
aaa
2 0
0 0
rc =
1 1
>> [aaaa rc] = popcorn(aaa)
aaaa =
2 0
0 1
rc =
2 2
>>aaaaa = popcorn(aaaa)
aaaaa =
2 1
0 1
This is a homework question so a nudge in the right direction would be greatly appreciated!

 Accepted Answer

Use 'randi' twice to generate random indices for the row and column and then add 1 there.

7 Comments

Hi Roger, I have tried this approach and this is the code i have so far.
function output = popcorn(M)
[w, z] = size(M);
a = randi(w);
b = a+1;
f = randi(z);
g = f+1;
output = [b, g];
This is a start i have so far, though it only gives me random numbers for the size of the matrix. Any other help would be greatly appreciated!
Rohan, you don't add 1 to the random row and column you get. You're supposed to add 1 to the value of w at that row and column, that is, w(a, f). Try again.
Hi there, Sorry to bother you again but I have tried this for so long now and still cant seem to get it. I have taken your advice and this is what is was able to come up with. function output = popcorn(M) [w, z] = size(M); a = randi(w); f = randi(z); b = w(a+1,f+1); output = b; end
But now when I run the code it now says
Attempted to access w(3,3); index out of bounds because numel(w)=1.
Error in popcorn (line 5) b = w(a+1,f+1);
Not sure how this happened.
Thank you for your help and time!
You are still adding one to each of the indices, and not to the value at that location. Look at the code w(a+1,f+1): a is an index, and then you add one to it. This is not the aim of your task! Image Analyst told you to not do this, but instead to "add 1 to the value of w at that row and column, that is, w(a, f). Try again." Note that Image Analyst even told you what you should be adding one to: w(a,f).
Try again!
Thank you Stephen for clarifying that! What i have done now is exactly what you said and also change w(a,f) to M(a,f)+ 1 as i need to index into the original matrix. This is the new code so far:
function output = popcorn(M)
[w, z] = size(M);
a = randi(w);
f = randi(z);
b = M(a,f)+1;
output = b;
end
Though now I only get a single value and now I'm wondering how to input that into the original matrix and keep 'popping pieces' in my matrix. Thank you for your help and time!
You need to declare a second output argument, rowAndColumn
function [output, rowAndColumn] = popcorn(M)
Now, what do you think, looking over your variables, rowAndColumn might be?
rowAndColumn = ??????
By the way, your "b" is totally unnecessary. And even worse , it's totally wrong . It needs to be the whole matrix, not just the one element. Keep trying.
Okay thank you, after a lot of thinking and reading what you said this is the new code, though it only gives a 1x2 matrix and not the matrix i need as per the example: function [output, rowAndColumn] = popcorn(M) [w, z] = size(M); a = randi(w); f = randi(z); rowAndColumn = [w,z]; output = [a,f]; end
I feel I'm getting closer to the answer yet i still feel so lost.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!