How to remove the third to last element of an array?

88 views (last 30 days)
Hello,
If I have an array of random numbers (e.g. number = [2; 3; 4; 5; 10; 0.45; . . . 3; 99; 294; 1.96] ), how can I remove the third to last value (99)? I need to remove it idependent of the value. I know how to remove the first element ( number(1) = [] ) and the last value ( number(end) = [] ), but I am not sure how to remove the position of the 99.
Does someone know how to do that? Any help will be appreciated.
Thanks,
Guilherme
  2 Comments
Guilherme de Melo
Guilherme de Melo on 31 Oct 2023
Moved: John D'Errico on 31 Oct 2023
The two solutions has been solved the question. Thanks. For last, if I do not wanna remove in a specific case and I use k=0:
%Number from last to delete
k=0;
%deletion
number(end-k+1) = []
Will it remove the last number if I define at the beguining of the code k=0? Or, how would be if I decide to remove multiple positions? e.g. the 8t and the third to last elements?
John D'Errico
John D'Errico on 31 Oct 2023
Please don't add an answer you to ask a question, even to your own question.
But, yes, if you wanted to remove specific elements, counting from the end backwards, you could use that scheme. Just define k to be the offset, or even multiple offsets from the end.

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 31 Oct 2023
number = [2; 3; 4; 5; 10; 0.45; 3; 99; 294; 1.96]
number = 10×1
2.0000 3.0000 4.0000 5.0000 10.0000 0.4500 3.0000 99.0000 294.0000 1.9600
%Number from last to delete
k=3;
%deletion
number(end-k+1) = []
number = 9×1
2.0000 3.0000 4.0000 5.0000 10.0000 0.4500 3.0000 294.0000 1.9600
  4 Comments
Dyuman Joshi
Dyuman Joshi on 31 Oct 2023
Yes, you are using a character vector.
When you use a character vector as an index, MATLAB converts the text to corresponding ascii values.
element_removed='[1 end-2]';
double(element_removed)
ans = 1×9
91 49 32 101 110 100 45 50 93
And these values are used as indices to perform operation(s).
Atleast one of the values is out of the range of the indices, thus you get the error.
The problem comes from the use of quotation marks. I do not know why you are using them.
The solution is to remove them.
y = primes(30)
y = 1×10
2 3 5 7 11 13 17 19 23 29
%No quotation marks used
y([1 end-2]) = []
y = 1×8
3 5 7 11 13 17 23 29
Also, you can not store "end" in a variable, you need to directly use it as an index.
Guilherme de Melo
Guilherme de Melo on 31 Oct 2023
Ok, thanks for explanation. I just trying to fix the paramens. The code to analysis signal is very long (>1200 lines) with some plot of figures, then I am checking the possibility to fix the positions of the values to remove at the beginnig of the code.

Sign in to comment.

More Answers (1)

Aquatris
Aquatris on 31 Oct 2023
Edited: Aquatris on 31 Oct 2023
% put the index to be removed in paranthesis, if you want to remove 99th
% index, put 99, otherwise you can use 'end' to mean 'length(a)' so 'end-1'
% would mean 1 before the last index
a = [1 2 3 4 5 6 7 8 9 0]; % array
a(end-2) = []; % remove '8' from the array
a
a = 1×9
1 2 3 4 5 6 7 9 0

Community Treasure Hunt

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

Start Hunting!