Code only works sometimes when run
Ältere Kommentare anzeigen
this code works some of the time i run it, but not others.
i.e. i and j will display some of the time when the script is run, but most often they will not
A = randi(5, 4, 4, 3);
size = numel(A);
n = randperm(size, 1);
[i j] = find(A==n);
disp([i,j])
any ideas why? cheers
4 Kommentare
Ideas? Sure. :)
Hint: What does numel(A) return?
PS. Don't use size as a variable; that aliases the builtin size() function and that will cause consternation elsewhere...
PPS. It works every time; it just illustrates that just having code that doesn't have any syntax error doesn't mean the logic is correct to solve the problem! :) While it is a necessary condition, it isn't sufficient.
We don't know what the actual intent was so we don't know what the actual solution would be.
Image Analyst
am 3 Sep. 2018
It's also a good idea not to use i and j because they are the built in imaginary variables.
Akzeptierte Antwort
Weitere Antworten (1)
Hi,
in addition to the valueable comments:
What you do is make a Matrix A with the dimensions: 4 x 4 x 3 which means it has 48 elements. These elements are randomly between 1...5
Then you create 1(!) random number n between 1...48
Then you want to get the indicies of all occurences from this one number in A.
The chance that n is in the Matrix = 5/48 ~ 10,42% --> Thats because you will only get indices back from the find function if n = [1...5]
At about 90% of all cases you test this, you will get nothin back, because n = [6...48]
I guess this is not the bahavior you wanted, but it is what you have written in your code.
Best regards
Stephan
1 Kommentar
Tom Seath
am 3 Sep. 2018
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!