delete element from vector

5.166 visualizzazioni (ultimi 30 giorni)
Majid Al-Sirafi
Majid Al-Sirafi il 24 Set 2012
Commentato: Sibghat il 2 Mar 2024 alle 9:04
Hi everyone
how can I delete element from vector .... for example
a=[1,2,3,4,5]
how can I delete 3 from above vector to be
a=[1,2,4,5]
thank you
majid
  7 Commenti
Rosie
Rosie il 5 Lug 2017
Modificato: Walter Roberson il 5 Lug 2017
Hi majed
You can use the follwoing
a(index)=[]
a(3)=[]
the number will delete
Good luck
Hamna Ameer
Hamna Ameer il 29 Set 2017
Modificato: Hamna Ameer il 29 Set 2017
a(3)=[] how can i directly store this in a new vector say b?

Accedi per commentare.

Risposta accettata

Daniel Shub
Daniel Shub il 24 Set 2012
Modificato: MathWorks Support Team il 9 Nov 2018
I can think of three ways that are all slightly different
a=[1,2,3,4,5];
If you want to get rid of all cases where a is exactly equal to 3
b = a(a~=3);
If you want to delete the third element
b = a;
b(3) = [];
or on a single line
b = a([1:2, 4:end]);
Or, as Jan suggests:
a = [2,3,1,5,4]
a(a == 3) = []
  6 Commenti
Walter Roberson
Walter Roberson il 5 Lug 2017
b = a(a >= 2 & a <= 4); %keep 2 to 4
Rik
Rik il 31 Mar 2021
@Anthony Dave Flags are not for personal bookmarks. Please remove your flag.

Accedi per commentare.

Più risposte (7)

Jan
Jan il 24 Set 2012
Modificato: Jan il 24 Set 2012
a = [1,2,3,4,5]
a(3) = []
Or:
a = [2,3,1,5,4]
a(a == 3) = []
These methods are explained exhaustively in the "Getting Started" chapters of the documentation. It is strongly recommended to read them completely. The forum is not though to explain the fundamental basics. Thanks.
  3 Commenti
Joel Bay
Joel Bay il 28 Giu 2019
"These methods are explained exhaustively in the "Getting Started" chapters of the documentation."
Wrong, definetely not exhaustively after comparing Daniel's answer and the documentation. Logical indexing is not even mentioned. The answers to this question is still useful in 2019.
irvin rynning
irvin rynning il 6 Dic 2021
unfortunately some of us prefer to use Matlab to solve problems in a timely manner, and cannot always engage in stackover-flow style plaudits on criticizing one's peers

Accedi per commentare.


masoud sistaninejad
masoud sistaninejad il 23 Ago 2021
A = [ 1 2 3 4 5 6 7]
A = 1×7
1 2 3 4 5 6 7
B = [1 3 6]
B = 1×3
1 3 6
C = setdiff(A,B)
C = 1×4
2 4 5 7
  2 Commenti
Andy Rojas
Andy Rojas il 24 Nov 2021
Thank you!
Emma Fickett
Emma Fickett il 29 Ott 2022
I've scoured through so many forums trying to remove a vector of values from another vector and setdiff does exactly what I needed, thank you so much!!

Accedi per commentare.


Andrei Bobrov
Andrei Bobrov il 24 Set 2012
a = a(abs(a - 3) > eps(100))

Will Reeves
Will Reeves il 15 Feb 2022
really crude, but if you wanted to remove a row defined by and index, rather than a value, you could do something like this:
function out=removeRow(in,index)
% removes a row from an matrix
[~,n]=size(in);
if index>n || index<0
error('index needs to be within the range of the data')
else
if n==1
out=[]; % you've removed the last entry
else
% strip out the required entry
if index==1
out=in(2:end);
elseif index==n
out=in(1:end-1);
else
out=in([1:index-1 index+1:n]);
end
end
end

Elias Gule
Elias Gule il 1 Dic 2015
% Use logical indexing
a = a(a~=3)
  2 Commenti
denny
denny il 31 Ago 2017
I like this answer.
Ntsakisi Kanyana
Ntsakisi Kanyana il 31 Mar 2020
Does it work on strings?

Accedi per commentare.


Abdul samad
Abdul samad il 4 Ago 2023
Modificato: Abdul samad il 4 Ago 2023
Yes , you can delete 3 from the given array by assigning the null matrix, like this .
In the command window do like this.
>> a=[1,2,3,4,5];
>> a(3) = [ ];
>>a
This will delete the 3 from the array a = [1,2,3,4,5];
Thank You

Sibghat
Sibghat il 2 Mar 2024 alle 8:36
The removal of the element at the 3rd index has already been addressed. However, if you want to remove all occurences of the number '3' from the array 'a', you can use the following code (with and without using the find method).
% For instance, let's modify the array 'a'
a = [1, 3, 2, 3, 4, 3, 5, 3];
b = find(a == 3); % Find the index of the element to delete
% The above line-of-code will also work without using the find keyword...
a(b) = []; % Delete the element(s)
a
a = 1×4
1 2 4 5
  1 Commento
Sibghat
Sibghat il 2 Mar 2024 alle 9:04
And if you want to store the removed values in another variable and display the the exact position of the value. You can do it by either replacing the other values with zeroes or by replacing the desired value with zeroes. Hopefully, the following code will help.
a = [1, 3, 2, 3, 4, 3, 5, 3];
indices_of_3 = find(a == 3); % Find indices of elements equal to 3
removed_values = a(a == 3); % Store the removed values in another variable named 'removed_values'
% Create a vector with zeroes where the number is 3
b = zeros(size(a));
b(a ~= 3) = a(a ~= 3);
% Create a vector with zeroes where the number is not 3
c = zeros(size(a));
c(indices_of_3) = a(indices_of_3);
% Remove all occurrences of 3 from 'original_vector'
a(a == 3) = [];
% Display the results
% Modified vector after removal of all occurrences of 3
a
a = 1×4
1 2 4 5
% Removed values
removed_values
removed_values = 1×4
3 3 3 3
% Displaying zero where values is 3
b
b = 1×8
1 0 2 0 4 0 5 0
% Displaying zero where value is not 3
c
c = 1×8
0 3 0 3 0 3 0 3

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by