Loop for removing values from a column vector

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

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

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

I'm getting this error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
It works for the example you've provided, as you can see.
The error sounds like your data is a column vector instead of a row vector. In that case, make logInd a column vector as well (use [1; ...] instead of [1 ...]).
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 = 6×1
1 2 3 7 8 9
Is it only for integers? It only seems to work until a certain part of the vector but it just doesn't what it's supposed to :/
It is not specific to integers, but it only looks at immediate neighbors. That could lead to a scenario where the final vector in not always strictly increasing.
I think a better solution would use cummax. This will give you the running max. I use unique to removed duplicates.
column_vector = rand(10,1)*10
column_vector = 10×1
2.1816 8.4985 1.3067 0.5397 3.1610 9.1202 7.1885 7.6020 9.8508 7.0116
new_column_vector = unique(cummax(column_vector))
new_column_vector = 4×1
2.1816 8.4985 9.1202 9.8508
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]
cummax worked like a charm, thank you!

Sign in to comment.

More Answers (1)

%%
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

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!