delete element from vector
조회 수: 4,678 (최근 30일)
이전 댓글 표시
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
Rosie
2017년 7월 5일
편집: Walter Roberson
2017년 7월 5일
Hi majed
You can use the follwoing
a(index)=[]
a(3)=[]
the number will delete
Good luck
Hamna Ameer
2017년 9월 29일
편집: Hamna Ameer
2017년 9월 29일
a(3)=[] how can i directly store this in a new vector say b?
채택된 답변
Daniel Shub
2012년 9월 24일
편집: MathWorks Support Team
2018년 11월 9일
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
추가 답변 (7개)
Jan
2012년 9월 24일
편집: Jan
2012년 9월 24일
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
Joel Bay
2019년 6월 28일
"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
2021년 12월 6일
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
masoud sistaninejad
2021년 8월 23일
A = [ 1 2 3 4 5 6 7]
B = [1 3 6]
C = setdiff(A,B)
댓글 수: 2
Emma Fickett
2022년 10월 29일
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!!
Will Reeves
2022년 2월 15일
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
댓글 수: 0
Abdul samad
2023년 8월 4일
편집: Abdul samad
2023년 8월 4일
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
댓글 수: 0
Sibghat
2024년 3월 2일
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
댓글 수: 1
Sibghat
2024년 3월 2일
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
% Removed values
removed_values
% Displaying zero where values is 3
b
% Displaying zero where value is not 3
c
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!