How do I randomly extract 20 segments of data from a signal?

2 views (last 30 days)
I have a matrix of data 60001x4. Rows are data points, columns are the 4 channels recording the data. How would I extract 20 random samples of 1000 data points from each channel? Goal would be to have a 20x1 cell array where each cell is a 1000x4 matrix representing 1 1000-sample segment across the 4 channels. I have done this so far but there must be a better way to do this to make it truly random segments. I simply divided the duration of my signal by 1000 to get how many (num_segments) I would end up with. Then i used randperm to generate 20 random numbers that would correspond to each segment of the 20 out of 60 segments, in this case, that I want.
random_indices = randperm(num_segments,20);

Accepted Answer

Star Strider
Star Strider on 18 Aug 2021
If I understand correctrly what you want to do, this could work:
signal = randn(60001,4)
signal = 60001×4
-0.2521 -1.3696 -3.9248 -1.4181 -0.3019 -1.7351 -0.4578 -1.4187 2.4288 0.3358 -0.2064 0.4342 -1.0420 0.6007 0.0606 0.4438 -0.3845 -0.2802 0.5768 -0.7815 0.4380 -1.7393 -1.0484 -1.6826 2.4050 -0.1283 -2.0273 0.8010 1.2604 1.2681 1.6034 -1.2695 -1.8335 0.8577 -0.0428 0.1045 -0.3216 -0.7440 -1.1730 -1.2357
random_indices = randperm(size(signal,1)-1000, 20)
random_indices = 1×20
46774 32626 2192 55305 11347 31821 18564 46263 17113 36835 11881 54189 27679 5805 58827 48480 37787 40739 8266 57593
rowidx = [random_indices(:) random_indices(:)+999]
rowidx = 20×2
46774 47773 32626 33625 2192 3191 55305 56304 11347 12346 31821 32820 18564 19563 46263 47262 17113 18112 36835 37834
for k = 1:numel(random_indices)
RandomSamples{k,:} = signal(rowidx(k,1):rowidx(k,2),:);
end
RandomSamples
RandomSamples = 20×1 cell array
{1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double} {1000×4 double}
Some of the samples would necessarilly overlap.
.

More Answers (1)

David Hill
David Hill on 18 Aug 2021
You could just keep matrix form with a 3rd dimension
for k=1:20
newMatrix(:,:,k)=yourMatrix(randperm(60001,1000),:);
end
  1 Comment
Anas Khan
Anas Khan on 19 Aug 2021
I believe this only extracts 1000 random numbers ranging from 1 to 60001. I need them to be continuous. Thanks for your input!

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!