delete element from vector

2,604 ビュー (過去 30 日間)
Majid Al-Sirafi
Majid Al-Sirafi 2012 年 9 月 24 日
コメント済み: Walter Roberson 2024 年 11 月 13 日
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
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
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
Daniel Shub 2024 年 11 月 13 日
編集済み: MathWorks Support Team 2024 年 11 月 13 日
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) = []
  5 件のコメント
kwabena boafo-mensah
kwabena boafo-mensah 2016 年 7 月 8 日
how does this work when i need to delete a range of row elements from a vector
Walter Roberson
Walter Roberson 2017 年 7 月 5 日
b = a(a >= 2 & a <= 4); %keep 2 to 4

サインインしてコメントする。

その他の回答 (7 件)

Jan
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.
  5 件のコメント
Keanu
Keanu 2024 年 6 月 12 日
A point of clarification for anyone who may be confused:
Consider the two arrays p = [10;20;30;40] and b = [10,20,30,40] (note the semicolon vs. comma) as an example. In this case, p(3) = [] and b(3) = [] will remove the third element from the array entirely, leaving p = [10;20;40] and b = [10,20,40].
If we were to mistakenly say p(3,1) = [] or b(1,3) = [], MATLAB will throw an error: "A null assignment can have only one non-colon index." Of course, this minor distinction will not be immediately clear to a beginner. Moreover, I do not expect anyone to understand this distinction from reading the "exhaustive" documentation.
The help forums are a guide to anyone with a legitimate question. To this day, I am puzzled by responses that jab at the author for merely asking.
Rik
Rik 2024 年 6 月 12 日
編集済み: Rik 2024 年 6 月 12 日
I'm surprised that is the error message you get, since it doesn't (at first glance at least) match the cause of the error, and yet:
p = [10;20;30;40];p(3,1) = []
A null assignment can have only one non-colon index.
But your comparison is strained, since your code has in indexing error, which is only superficially related to the deletion of array elements.
The only problem with this question is that it should be covered by any half-decent tutorial, perhaps in the first 15 minutes even. In addition to this, you can find extra information in the documentation. My personal bar is that you shouldn't be able to enter the question in Google and get the solution in the first result.

サインインしてコメントする。


masoud sistaninejad
masoud sistaninejad 2021 年 8 月 23 日
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 件のコメント
Andy Rojas
Andy Rojas 2021 年 11 月 24 日
Thank you!
Emma Fickett
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!!

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2012 年 9 月 24 日
a = a(abs(a - 3) > eps(100))
  1 件のコメント
Majid Al-Sirafi
Majid Al-Sirafi 2012 年 9 月 24 日
than you very much

サインインしてコメントする。


Will Reeves
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

Elias Gule
Elias Gule 2015 年 12 月 1 日
% Use logical indexing
a = a(a~=3)
  3 件のコメント
Ntsakisi Kanyana
Ntsakisi Kanyana 2020 年 3 月 31 日
Does it work on strings?
Walter Roberson
Walter Roberson 2024 年 11 月 13 日
a = ["this", "is", "a", "test"];
a = a(a ~= "is")
a = 1x3 string array
"this" "a" "test"

サインインしてコメントする。


Abdul samad
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

Sibghat
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
a = 1×4
1 2 4 5
  1 件のコメント
Sibghat
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
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming Utilities についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by