How to create a function that returns a matrix with one more popped piece randomly added and the location of it.
2 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Roger Stafford
on 25 Jan 2015
Use 'randi' twice to generate random indices for the row and column and then add 1 there.
7 Comments
Image Analyst
on 25 Jan 2015
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.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!