How to remove/add elements to an array?

39 Ansichten (letzte 30 Tage)
Susan
Susan am 13 Dez. 2022
Kommentiert: Voss am 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) = []

Akzeptierte Antwort

Voss
Voss am 13 Dez. 2022
Bearbeitet: Voss am 13 Dez. 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 Kommentare
Susan
Susan am 3 Jan. 2023
@Voss thank you so much for your help!
Voss
Voss am 3 Jan. 2023
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

millercommamatt
millercommamatt am 13 Dez. 2022
Bearbeitet: millercommamatt am 13 Dez. 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 Kommentare
Voss
Voss am 13 Dez. 2022
@millercommamatt: What happens when B has two elements in a row > 1500?
A = [0 1600 3400];
B = diff(A);
inds = find(B>1500);
for ii = inds
A = [A(1:ii), (A(ii)+A(ii+1))/2, A(ii+1:end)];
end
A
A = 1×5
0 800 1200 1600 3400
Doesn't seem right. That's because when you insert the new element, all the higher indices (i.e., the rest of the elements of inds) are now one lower than they need to be, to account for the new element just inserted.
If you start at the end and go backwards instead, it works:
A = [0 1600 3400];
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 = 1×5
0 800 1600 2500 3400
millercommamatt
millercommamatt am 13 Dez. 2022
this is why you test code

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by