フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

help I need matlab code I have this condition m=find(distance<=r & distance~=0) after we get m values as a row vector I need to choose one of m's value randomly

1 回表示 (過去 30 日間)
maha ismail
maha ismail 2015 年 1 月 5 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
help I need matlab code I have this condition
m=find(distance<=r & distance~=0)
after we get m values as a row vector I need to choose one of m's value randomly

回答 (1 件)

dpb
dpb 2015 年 1 月 5 日
n=m(randi(length(m),1));
doc randi % for details...
  8 件のコメント
Image Analyst
Image Analyst 2015 年 1 月 7 日
You both get the length, but then he directly goes to get an integer-valued double in the range of 1 to the length. You get a double random number in the range 0-1, then multiply it by the length to get a double in the range of 0 to length, then call ceil() to round it up to the next highest integer (though it's still a double). So it's similar but yours requires a multiplication and rounding-up step that his does not (though maybe that might be done internally, hidden to you). So because his code uses the function made for this and doesn't have two other operations that you have to do yourself manually, that's why I think his method is better.
dpb
dpb 2015 年 1 月 7 日
...how it does work?
See
doc randi % for the details.
It uses an internal TMW-supplied routine for the purpose that, as IA notes, puts the details at a compiled level out of view of the top-level code.
>> which randi
built-in (C:\ML_R2012b\toolbox\matlab\randfun\randi)
>>
When TMW goes to the trouble to build routines as compiled, built-ins, it behooves one to use them, especially when they're directly applicable.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by