Code only works sometimes when run

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

dpb
dpb 2018 年 9 月 3 日
編集済み: dpb 2018 年 9 月 3 日
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.
Stephen23
Stephen23 2018 年 9 月 3 日
@Tom Seath: do NOT call any variable size, as this will shadow the very important inbuilt size function.
Tom Seath
Tom Seath 2018 年 9 月 3 日
編集済み: Tom Seath 2018 年 9 月 3 日
I actually haven't been using the variable size, i just changed it to paste the code here, but thanks for the tip.
Image Analyst
Image Analyst 2018 年 9 月 3 日
It's also a good idea not to use i and j because they are the built in imaginary variables.

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

 採用された回答

Stephen23
Stephen23 2018 年 9 月 3 日
編集済み: Stephen23 2018 年 9 月 3 日

2 投票

"any ideas why?"
Because you test for numbers that do not exist in A, and so the output is empty.
Lets have a look at an example:
>> A = randi(5, 4, 4, 3) % 4x4x3 array of values 1-5.
A(:,:,1) =
1 3 1 3
4 3 1 1
1 1 5 4
5 3 5 3
A(:,:,2) =
1 2 1 1
5 3 1 5
1 5 2 5
1 1 2 4
A(:,:,3) =
4 2 3 1
5 2 2 1
2 4 2 3
5 2 5 4
>> S = numel(A) % do NOT use SIZE as a variable name!
S = 48
>> n = randperm(S,1) % could be 1-48.
n = 7
But are any of the values in A equal to 7? Of course not, because you specified that the values of A must be 1-5. So for most random values of n (from 6-48), there will be absolutely no matches:
>> A==n
ans(:,:,1) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
ans(:,:,2) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
ans(:,:,3) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> find(A==n)
ans = []
What do you expect to happen when you test for values that do not exist?

1 件のコメント

Tom Seath
Tom Seath 2018 年 9 月 3 日
Thank you very much for the example. Well explained

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

その他の回答 (1 件)

Stephan
Stephan 2018 年 9 月 3 日
編集済み: Stephan 2018 年 9 月 3 日

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

リリース

R2018a

質問済み:

2018 年 9 月 3 日

編集済み:

dpb
2018 年 9 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by