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

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

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

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

"Will it remove the last number if I define at the beguining of the code k=0?"
No, if k==0, then index is end+1, for which an element does not exist. And you can not delete something that does not exist.
Also, 0th number from last does not make sense.
You can delete multiple elements at once by using indexing -
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
%delete multiple elements at once
number([3 end-2]) = []
number = 8×1
2.0000 3.0000 5.0000 10.0000 0.4500 3.0000 294.0000 1.9600
Ok. I created a varible to refer to the number to removal and it is showing an error:
%element to be removed from the last in results
element_removed='[1 end-2]';
or it shows also error if I try:
element_removed='end-2';
line 619:
back_azm(element_removed)=[];
error:
Matrix index is out of range for deletion.
Error in source_location_updated_test_up (line 619)
back_azm(element_removed)=[];
Do you know why?
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.
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)

% 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

Categories

Community Treasure Hunt

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

Start Hunting!