How to remove/add elements to an array?

11 views (last 30 days)
Susan
Susan on 13 Dec 2022
Commented: Voss on 3 Jan 2023
Hi All,
I have a 115x1 vector, say A which is attached, that I'd like to remove the elements that satisfy some conditions and add some that satisfy others. To be specific,
B = diff(A) = [768 12 757 767 12 756 ...1524.....768 13 754 271]
if B(n)<100, I would like to remove A(n) from A.
if 100 < B(n) < 300, I would like to remove A(n+1) from A.
if B(n) > 1500, I'd like to add an element to A between two elements of A that their diff value satisfies this constraint. for example B(91)=1523, and I like to add a point between A(91) and A(92), i.e., A(91)+A(92)/2.
I know how to do that when there is one constraint, but I struggle when there are more. Any help would be greatly appreciated.
pointToDelet = B<100;
A(pointToDelet) = []

Accepted Answer

Voss
Voss on 13 Dec 2022
Edited: Voss on 13 Dec 2022
load locs
A = locs;
B = diff(A);
idx = B < 100;
A(idx) = []; % remove from both A and B because
B(idx) = []; % you need both A and B for the next step
idx = [false; B > 100 & B < 300];
A(idx) = [];
B(idx(2:end)) = [];
inds = find(B > 1500);
for ii = inds(end:-1:1).'
A = [A(1:ii); (A(ii)+A(ii+1))/2; A(ii+1:end)];
end
@Susan: By the way, what should happen if an element of B is exactly 100? According to the rules, if it were slightly less than 100 then the corresponding element of A would be removed; if it were slightly more than 100 then the element of A after it would be removed; but if it's exactly 100, A is unchanged...
Regardless of that, notice that after this process, you can still end up with elements of diff(A) = B > 1500, because the insertion of the new element in A effectively halves the two corresponding elements in B (i.e., the difference is now split half and half among two adjacent elements), but if the original difference was sufficiently large (> 3000), half of it will still be > 1500. An example of this (I don't know if it's a problem for what you're trying to do, but it's something to be aware of):
A = [0; 5000; 11000];
B = diff(A);
inds = find(B > 1500);
for ii = inds(end:-1:1).'
A = [A(1:ii); (A(ii)+A(ii+1))/2; A(ii+1:end)];
end
A
A = 5×1
0 2500 5000 8000 11000
B = diff(A)
B = 4×1
2500 2500 3000 3000
  4 Comments

Sign in to comment.

More Answers (1)

millercommamatt
millercommamatt on 13 Dec 2022
Edited: millercommamatt on 13 Dec 2022
% B = diff(A) = [768 12 757 767 12 756 ...1524.....768 13 754 271]
% note that B will be one less in length than A
% if B(n)<100, I would like to remove A(n) from A.
A_FilteredOnB = A(1:end-1);
A_FilteredOnB = A_FilteredOnB(B>=100);
% if 100 < B(n) < 300, I would like to remove A(n+1) from A.
A_FilteredOnB_again = A(2:end); % note the difference from before
A_FilteredOnB_again = A_FilteredOnB_again(B >= 100 & B <= 300);
% if B(n) > 1500, I'd like to add an element to A between two elements
% of A that their diff value satisfies this constraint. for example B(91)=1523,
% and I like to add a point between A(91) and A(92), i.e., A(91)+A(92)/2.
% This one is tricky.
inds = find(B>1500);
for ii = inds
A = [A(1:ii), (A(ii)+A(ii+1))/2, A(ii+1:end)];
end
  2 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by