keep element greater than immediate previous element

조회 수: 15 (최근 30일)
HYZ
HYZ 2020년 5월 24일
Hi,
x = [1 2 3 4 3 2 3 4 6 8 5 5 6 8.5 9 11 12 ]; I want to keep one vector whereby the next element is greater than the immediate previous element. keep = [[1 2 3 4 6 8 8.5 9 11 12 ]; to discard if the element is smaller than or equal to the immediate previous element. discard = [3 2 3 4 5 5 6] (the bold elements in x).
I am using loop to do that and I didn't get what I want. could help modify my code or advise any alternative way? thanks in advance!
clc; clear; close all
x = [1 2 3 4 3 2 3 4 6 8 5 5 6 8.5 9 11 12 ];
m = length(x);
k=1; n = 1; i =1; j=2;
while i < m-1 & j < m
if x(j) >= x(i)
keep(n) = x(j);
n=n+1;
i=i+1;
j=j+1;
elseif x(j) < x(i)
discard(k) = x(j);
k = k+1;
i=i;
j=j+1;
end
end

채택된 답변

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 5월 24일
This loops does what you want:
x = [1 2 3 4 3 2 3 4 6 8 5 5 6 8.5 9 11 12 ];
m = length(x);
keep = [x(1)];
discard =[];
for idx=2:m
if x(idx)>keep(end)
keep = [keep,x(idx)];
else
discard = [discard,x(idx)];
end
end
keep
discard
keep =
1.0000 2.0000 3.0000 4.0000 6.0000 8.0000 8.5000 9.0000 11.0000 12.0000
discard =
3 2 3 4 5 5 6
Although, depending of your goal, simply sorting the array could solve the problem in a more simplified way:
keep = unique(sort(x))
keep =
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 8.0000 8.5000 9.0000 11.0000 12.0000
  댓글 수: 2
HYZ
HYZ 2020년 5월 24일
편집: HYZ 2020년 5월 24일
Can I ask another question? I am trying the way you suggested. my data are in cell arrays. X is a cell array which has a few vectors. I want to use the loop you suggested for each vector in cell array. I got an error "Operator '>' is not supported for operands of type 'cell'" ... could you suggest why > doesn't work in cell array? Thanks.
for i = 1: length(X)
keep{i}(1) = [X{i}(1)];
for idx = 2: length(X{1})
if X{i}(idx) > keep{i}(end)
keep{i} = [keep{i},X{i}(idx)];
end
end
end
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 5월 24일
In mine matlab version it works fine, it can be that older versions have some problems with double indexing in cell array. One thing you could try is to remove the variables from the cell whenever possible until you get no error anymore, ex:
x1 = [1 2 3 4 3 2 3 4 6 8 5 5 6 8.5 9 11 12 ];
x2 = [1 2 3 4 3 2 3 4 6 8 5 5 6 8.5 9 11 12 ];
X = cell(2,1);
X{1} = x1;
X{2} = x2;
keep = cell(2,1);
for i = 1: length(X)
keep{i}(1) = [X{i}(1)];
actualX = X{i};
for idx = 2:length(actualX)
actualkeep = keep{i};
actualkeep = actualkeep(end);
if actualX(idx) > actualkeep
keep{i} = [keep{i},X{i}(idx)];
end
end
end
Although I would remain with the vectorized solution I suggested if all you want is the keep array

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

추가 답변 (1개)

per isakson
per isakson 2020년 5월 24일
An alternative
%%
x = [1 2 3 4 3 2 3 4 6 8 5 5 6 8.5 9 11 12 ];
%%
dx = 1;
while not( isempty( dx ) )
dx = find((diff(x)<=0));
x(dx+1)=[];
end
disp( x )
output
Columns 1 through 5
1 2 3 4 6
Columns 6 through 10
8 8.5 9 11 12
>>

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by