# Vector manipulation for randomized data

1 view (last 30 days)
Elijah L on 2 Nov 2020
Edited: Sourabh Kondapaka on 6 Nov 2020
I have generated a vector "SleepData" that creates a 7200x1 vector of random #s from 0-12. I want the vector sensor data to have the value 1 when SleepData > 4. I have been able to do this.
I need help to make it so that the value of 1 lasts for 300 more indicies following the each initiation of 1. How can I do this?
% When sleep data >= 4cm , Have SENSOR = 1 (ON)
% If no data >=4cm for 300s, Have SENSOR = 0 (OFF)
% Display on plot when SENSOR is on or off and it's duration
SENSOR = zeros(length(SleepData),1);
SENSOR(SleepData>=4) = 1;
##### 2 CommentsShowHide 1 older comment
Elijah L on 2 Nov 2020
This works but only for the first initiation of the value 1. Everytime that SENSOR =1, I want it to "hold" the value for 300 intances. How would I do this?

Sourabh Kondapaka on 6 Nov 2020
Edited: Sourabh Kondapaka on 6 Nov 2020
You are right in suggesting S. Walter's code would only update for the first occurance of 1. In the below code, I added a for-loop which will do the desired operation for all the occurances of 1's. Only the occurances of 1's before performing the desired operation(making the next 300 index values 1's) will be considered.
clearvars
clc
close all
% Make a random vector
SleepData = randi(12,[7200,1]);
% Make a vector of 0's
SENSOR = zeros(size(SleepData));
% When SleepData is equal to or greater than 4, set it to 1
SENSOR(SleepData>=4) = 1;
% Get all indices where SENSOR value is 1.
indices = find(SENSOR==1);
% This for loop will iterate over the occurance of 1s which occured before performing the
% desired operation(making the next 300 index values 1)
%Iterate over each of the occurance and make the next 300 index values 1
for i = 1:length(indices)
SENSOR(indices(i): indices(i)+300) = 1;
end