Random picking of adjacent data points

Hi,
May someone help me
I have data in one column and want to randomly pick adjacent 48 data points for 1000 times. For example, we have 10 data point (1, 2,3, 4,5,6,7,8,9,10) and we randomly pick 3 adjacent data points 5 times.
May some one help me ...
Thank you.

 採用された回答

Image Analyst
Image Analyst 2020 年 8 月 29 日

0 投票

Try this:
data = [1:50000]'; % Whatever.....
% Define general parameters for getting the samples:
numElements = size(data, 1)
numAdjacent = 48
numSubsets = 1000
% Now do the iterations to get each of the 1000 subsets.
for k = 1 : numSubsets
% Get the first index at random. Don't get within numAdjacent of the end though!
firstIndex = randi(numElements - numAdjacent + 1);
% Now get the second index which will make the subset have adjacent indexes.
secondIndex = firstIndex + numAdjacent - 1;
% Get one subset of 48 adjacent data samples.
theseData = data(firstIndex : secondIndex);
% Now do something with this set of adjacent elements.
end

5 件のコメント

aa
aa 2020 年 8 月 29 日
Thank you this work very well and serve for the purpose, but loop did not work ... even if i delete the loop I get same results.
Image Analyst
Image Analyst 2020 年 8 月 29 日
I actually ran it before I posted it, so I know the loop works. Perhaps you modified it somehow, however you forgot to post the altered code that doesn't work. Please post the error - ALL the red text -- if there is any, along with the non-working code.
aa
aa 2020 年 8 月 29 日
Thnk you for explaining this, Yes, code works well there is no error but it did not clculate for the whole loop. This gives just one column of 48 data point but i required 1000 columns.
Image Analyst
Image Analyst 2020 年 8 月 29 日
You said "I have data set of 50000 enteries and I want to pick 1000 random subsets having 48 adjacent enteries." You did not tell us that you wanted those subsets stored in a 2-D array. Well I'm sure you've already done it by now, but anyway, here is how I created a matrix of 1000 columns and put each subset into its appropriate column:
data = [1:50000]'; % Whatever.....
% Define general parameters for getting the samples:
numElements = size(data, 1)
numAdjacent = 48
numSubsets = 1000
% Create an output array with 1000 columns.
output = zeros(numAdjacent, numSubsets);
% Now do the iterations to get each of the 1000 subsets.
for k = 1 : numSubsets
% Get the first index at random. Don't get within numAdjacent of the end!
firstIndex = randi(numElements - numAdjacent);
% Now get the second index which will make the subset adjacent.
secondIndex = firstIndex + numAdjacent - 1;
fprintf('Subset %d goes from index %d to index %d.\n', k, firstIndex, secondIndex);
% Get one subset of 48 adjacent data samples.
theseData = data(firstIndex : secondIndex);
% Now store this set of adjacent elements into our output array.
output(:, k) = theseData;
end
aa
aa 2020 年 8 月 30 日
Thank you, THis works very well and serve the purpose perfectly.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 8 月 29 日
編集済み: madhan ravi 2020 年 8 月 29 日

0 投票

datasets = num2cell(reshape(data(randperm(48e3)), [], 1e3), 1);
%celldisp(datasets)

3 件のコメント

aa
aa 2020 年 8 月 29 日
thanks for this, but this not work in my case, i want to get adjacent enteries. For example, I have data set of 50000 enteries and I want to pick 1000 random subsets having 48 adjacent enteries.
Hope this i conveny you exactly.
madhan ravi
madhan ravi 2020 年 8 月 29 日
Edit your question a billion times to each new question.
aa
aa 2020 年 8 月 29 日
Apology, I may not explain what i exactly want ... This may explain my question in better way .....

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

カテゴリ

ヘルプ センター および File ExchangeDescriptive Statistics and Insights についてさらに検索

質問済み:

aa
2020 年 8 月 29 日

コメント済み:

aa
2020 年 8 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by