Info

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

Select numbers from a vector under a specific condition

1 回表示 (過去 30 日間)
Johan Johan
Johan Johan 2019 年 11 月 30 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have this code
for i=1:5;
minValue = 0.0251;
maxValue = 3.9811 / 5; % Divide by 5 to ensure sum of 5 is less than 3.9811
randomValues = 10*(minValue + (maxValue - minValue) * rand(1,i));
end
A=randomValues;
I want to select from vector A two random value in which this values it is verify this condition
1- such one it is between 1 and 5.
for example a and b two values, the condition is
m=1;
n=5;
(a>=m && a<=n) && (b>=m && b<=n)
  1 件のコメント
dpb
dpb 2019 年 11 月 30 日
編集済み: dpb 2019 年 11 月 30 日
minValue = 0.0251;
maxValue = 3.9811 / 5; % Divide by 5 to ensure sum of 5 is less than 3.9811
A=10*(minValue + (maxValue - minValue) * rand(1,5));
Your code doesn't return a vector; it just generates one value each pass thru the loop and overwrites the previous with the current...

回答 (2 件)

Joe Vinciguerra
Joe Vinciguerra 2019 年 11 月 30 日
編集済み: Joe Vinciguerra 2019 年 11 月 30 日

David Hill
David Hill 2019 年 11 月 30 日
B=A(A>=m & A<=n);%finds all numbers between m and n in A
C=B(randperm(length(B),2));%chooses 2 numbers randomly from B

Community Treasure Hunt

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

Start Hunting!

Translated by