フィルターのクリア

How i can save a random value from a matrix in a variable,exluding values lower than 1?

3 ビュー (過去 30 日間)
I have a matrix 100X100 and i want a command to insert in variable pnode a random value of this matrix but,i want the value to be bigger than 1.For example if i have the matrix A=[4 5 2 1; 8 3 1 0] my variable pnode,can take on of the values 4,5,2,8,3 randomly

採用された回答

Image Analyst
Image Analyst 2015 年 4 月 22 日
Try this:
% Generate sample data
A=[4 5 2 1; 8 3 1 0]
% Get column vector of all the elements where A > 0
A_No_Zeros = A(A > 0)
% Get a random index.
randomIndex = randi(length(A_No_Zeros), 1);
% Assign pnode the random value from A_No_Zeros.
pnode = A(randomIndex)
  4 件のコメント
Giannakis Stoukas
Giannakis Stoukas 2015 年 4 月 22 日
Well i followed the commands you wrote,but after a number of repeats my value takes the number one.Although at the results it writes that the minimum value of A_No_Zeros is value 2.I really dont get why this happens.
Image Analyst
Image Analyst 2015 年 4 月 22 日
Because once you ignore 0's and 1's in [4 5 2 1; 8 3 1 0] you are left with [4 5 2 8 3] and the minimum of that is 2. Not sure why you're surprised. However my code did not compute the minimum so that must be code you added.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by