Delete multiple elements from matrix, that match value at once

78 ビュー (過去 30 日間)
Marc Laub
Marc Laub 2020 年 5 月 14 日
編集済み: Stephen23 2020 年 5 月 14 日
Hello everybody,
I wanted to know if there is a possibility do remove certain elements from a matrix at once, that match a specific value, without using the indexes?
You can do it with single values like this:
A(A==cerain_value)=[];
or
A(A<cerain_value)=[];
But what if there are multiple values I want to have removed?
Like :
A=[1,2,3,4,5;3,4,6,7,8;1,2,4,5,6;8,7,6,3,4;1,2,3,4,5];
and I want to remove every 1,3 and 5, without any loop or going with the indexes instead of the values.
How is this possible?
A(A==[1,2,5])=[];
does not work.
Many thanks in advance.
Best regards
Marc

採用された回答

Stephen23
Stephen23 2020 年 5 月 14 日
編集済み: Stephen23 2020 年 5 月 14 日
The simple MATLAB solution is to use ismember to generate the indices:
>> A = [1,2,3,4,5;3,4,6,7,8;1,2,4,5,6;8,7,6,3,4;1,2,3,4,5];
>> A(ismember(A,[1,3,5])) = []
A =
8
2
4
2
7
2
6
4
6
4
7
4
8
6
4

その他の回答 (1 件)

Mehmed Saad
Mehmed Saad 2020 年 5 月 14 日
編集済み: Mehmed Saad 2020 年 5 月 14 日
A=[1,2,3,4,5;3,4,6,7,8;1,2,4,5,6;8,7,6,3,4;1,2,3,4,5];
R = [1 2 5];
L=arrayfun(@(x) A==x,R,'uni',0);
A(any(cat(3,L{:}),3)) = [];

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by