delete elements from the given array
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Oksi Moon
am 1 Dez. 2020
Kommentiert: Stephan
am 1 Dez. 2020
for example. an array X is given, which is X=[1,2,3,4,5,6] how to delete all even numbers of this array except the last even number(6)? In other words, how to make this array look like X=[1,3,5,6]?
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 1 Dez. 2020
Try this
X=[1,2,3,4,5,6];
idx = find(mod(X,2)==0);
X(idx(1:end-1)) = []
2 Kommentare
Stephan
am 1 Dez. 2020
X=[1,2,3,4,5,6,7]
idx = find(mod(X,2)==0);
X(idx(1:end-1)) = []
results in:
X =
1 2 3 4 5 6 7
X =
1 3 5 6 7
you stated:
"...what if I have 7 elements [1,2,3,4,5,6,7] and I still need to get [1,3,5,6]. i need to leave the last even number. not just the last number"
Siehe auch
Kategorien
Mehr zu Logical 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!