How to create a function that returns a matrix with one more popped piece randomly added and the location of it.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Rohan Nair
il 25 Gen 2015
Commentato: Rohan Nair
il 25 Gen 2015
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 Commenti
Risposta accettata
Roger Stafford
il 25 Gen 2015
Use 'randi' twice to generate random indices for the row and column and then add 1 there.
7 Commenti
Image Analyst
il 25 Gen 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!