How to select values in a vector

28 views (last 30 days)
Hello,
My vector has sets of values every certain time (this time is variable) and I would like a new vector (with the same size as the 'posicaofinal' vector) with only the first two values of each set of values. For example: If I have 8 values, I only choose the first 2. If I have 1 value (this happens), I discard this value from the new array. Basically I'm just picking the first pairs of values.
Thanks in advance.

Answers (1)

Sajid Afaque
Sajid Afaque on 16 Feb 2022
Edited: Sajid Afaque on 16 Feb 2022
hello ,
as i have understood from your description i have framed a solution.
Yes for sure the below code can be optimised further, but ill leave this task for you.
Hope this helps
%make a copy of your data and work on that
copy_data = posicaofinal;
count = 1; % number of successive non adjacent number you need, here you need two non zero numbers from a group
for i = 1 : 1 : numel(copy_data)
if copy_data(i) == 0
count = 1;
continue;
else
%check whether the adjacent values of the current data are 0 i.e.
%this group has only one non zero value
if i == 1
singularity_condition = (copy_data(i+1) == 0);
elseif i == numel(copy_data)
singularity_condition = (copy_data(i-1) == 0);
else
singularity_condition = (copy_data(i+1) == 0 && copy_data(i-1) == 0);
end
%below code discards single non zero value and if there are group
%of non zeroes together , then replace all the values of the group
%except for starting two values
if singularity_condition
copy_data(i) = 0;
else
if count < 3
count = count+1;
continue;
else
copy_data(i) = 0;
end
end
end
end
%copy_data is your new data with same length as posicaofinal

Categories

Find more on Argument Definitions in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!