How to replace zeros in a vector with random segments of the same vector?
Show older comments
I have a vector with 240000 data points. There are several, 72 elements long segments of zero values in that vector. Now I would like to replace these segments of zeros with random 72 elements long segments from the remaining non zero part of the vector.
Can you help me how can I do it?
Accepted Answer
More Answers (1)
try this:
A=[1 2 3 0 0 0 4 5 0 0 0 0 0 0 0 0 8 9]'; % any matrix
B=A(A==0); % extract the number of 0
rdnumber=randi([10 20],size(A,2),numel(B)); % generating random number
A(A==0)=rdnumber % replace with random number
1 Comment
Image Analyst
on 24 Mar 2022
That is not using only the non-zero values to plug the zeros, like I do in my answer. You're including numbers not in the original vector.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!