how can slect randomly numbers from two array s

1 回表示 (過去 30 日間)
usman younus
usman younus 2017 年 6 月 28 日
回答済み: Gopichandh Danala 2017 年 6 月 28 日
A=[3818.36,7632.40,11446.44,15260.48,19074.52];
B=[657.15,1089.15,1521.15,1953.15,2385.15];
I have two arrays with 5 elements each and want to select 5 numbers randomly from both arrays, please help me how can select any 5 number from both A & B
  1 件のコメント
Jan
Jan 2017 年 6 月 28 日
With repetitions or without?

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

採用された回答

Gopichandh Danala
Gopichandh Danala 2017 年 6 月 28 日
A=[3818.36,7632.40,11446.44,15260.48,19074.52];
B=[657.15,1089.15,1521.15,1953.15,2385.15];
vals = zeros(1,length(A));
for i = 1:length(A)
idx = randperm(2,1);
if idx == 1
vals(i) = A(i);
else
vals(i) = B(i);
end
end
disp(vals)

その他の回答 (2 件)

Adam
Adam 2017 年 6 月 28 日
編集済み: Adam 2017 年 6 月 28 日
idx = randperm(10,5);
C = [A, B];
selectedNums = C(idx);
or to be a little more generic and avoid ugly hard-coded numbers that can be taken from the data instead:
C = [A, B];
idx = randperm(numel(C),5);
selectedNums = C(idx);
  1 件のコメント
usman younus
usman younus 2017 年 6 月 28 日
A=[3818.36,7632.40,11446.44,15260.48,19074.52]; B=[657.15,1089.15,1521.15,1953.15,2385.15];
Adam i need one value from each index of A&B justlike on index 1, I need 3818.36 or 657.15, and from index2 i need 7632.40 0r 1089.15 but your suggested code is giving 5 values from all values of A&B please hlep

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


Andrei Bobrov
Andrei Bobrov 2017 年 6 月 28 日
編集済み: Andrei Bobrov 2017 年 6 月 28 日
A=[3818.36,7632.40,11446.44,15260.48,19074.52];
B=[657.15,1089.15,1521.15,1953.15,2385.15];
C = [A;B];
out = C(sub2ind(size(C),randi([1,2],1,5),1:5));

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by