Loop for removing values from a column vector

9 views (last 30 days)
Hi all!
So I have a column vector that I imported from Excel spreadsheet with a certain amount of values (3000 something). What I'm trying to do is create a script with a loop that will go through every value in the vector and if the value of element k is <= than the value of element k-1, it deletes this value and goes to the next one.
Example: column_vector = [1 2 1 3 7 7 5 4 8 6 9 2] -------------> new_column_vector = [1 2 3 7 8 9]
Basicaly I want it to go up withou oscilating. Any help is appreciated (I havent touched Matlab for at least 5 years)
  1 Comment
Nikita Kaminskyy
Nikita Kaminskyy on 10 Dec 2020
Edited: Nikita Kaminskyy on 10 Dec 2020
An important remark: my column_vector has values with several decimal places (10 or so), they're all double values

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 10 Dec 2020
I would do this with logical indexing.
column_vector = [1 2 1 3 7 7 5 4 8 6 9 2];
logInd = [1 diff(column_vector)]>0; % Since diff will have n-1 values, prepend with 1
new_column_vector = column_vector(logInd)
new_column_vector = 1×6
1 2 3 7 8 9
  6 Comments
Nikita Kaminskyy
Nikita Kaminskyy on 10 Dec 2020
I guess there must have been a misunderstanding of the problem. Your code is calculating the difference from two consecutive values and in fact it works for the given example. Imagine I have the following vector:
v = [200.880438000000
201.080292400000
201.264694400000
201.461538600000
197.282910400000
197.743862800000
198.381722000000
198.644310200000
198.974400800000
199.502214800000
200.040847000000
200.149417000000
200.473740600000
201.053679000000
201.173782400000
201.330348800000]
The output I'm expecting is:
new_v = [200.880438000000
201.080292400000
201.264694400000
201.461538600000
201.583782400000
201.690348800000]

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 10 Dec 2020
%%
v=[1 2 1 3 7 7 5 4 8 6 9 2];
for k=length(v):-1:2
if v(k)<=v(k-1)
v(k)=[];
end
end

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!