필터 지우기
필터 지우기

How to select values in a vector

조회 수: 32 (최근 30일)
Jórdan Venâncio Leite
Jórdan Venâncio Leite 2022년 2월 16일
댓글: Jórdan Venâncio Leite 2022년 2월 17일
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.

답변 (1개)

Sajid Afaque
Sajid Afaque 2022년 2월 16일
편집: Sajid Afaque 2022년 2월 16일
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
  댓글 수: 2
Sajid Afaque
Sajid Afaque 2022년 2월 17일
@Jórdan Venâncio Leite did that work ?
Jórdan Venâncio Leite
Jórdan Venâncio Leite 2022년 2월 17일
Hi @Sajid Afaque I haven't tested it yet

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by