removing rows containing certain values

157 Ansichten (letzte 30 Tage)
Seth
Seth am 28 Mär. 2016
Bearbeitet: Kirby Fears am 28 Mär. 2016
i have a 3X3 matrix and i want to delete rows containing values greater than a certain value and values less than another certain value, then store the deleted rows separately. These values lie on the second and third column. thank you

Antworten (2)

Kirby Fears
Kirby Fears am 28 Mär. 2016
Bearbeitet: Kirby Fears am 28 Mär. 2016
Seth,
You can create logical indexes with conditional statements, and use the & for "and" as well as | for "or" to combine conditions. The resulting logical index can be used for indexing. Here's an example:
m = magic(3);
idx = (m > 8) | (m < 3);
% using "find" to identify rows where logical idx is true
[delRows,~] = find(idx);
% remove duplicates
delRows = unique(delRows);
% store deleted rows
m_toDelete = m(delRows,:);
% delete rows
m(delRows,:) = [];
Hope this helps.
  1 Kommentar
Kirby Fears
Kirby Fears am 28 Mär. 2016
Bearbeitet: Kirby Fears am 28 Mär. 2016
The method from Azzi's answer is more efficient:
m = magic(3);
idx = any((m > 8) | (m < 3),2);
% store deleted rows
m_toDelete = m(idx,:);
% delete rows
m(idx,:) = [];

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 28 Mär. 2016
Bearbeitet: Azzi Abdelmalek am 28 Mär. 2016
A=rand(3,3)
idx=any(A<0.1 | A>0.9,2)
out=A(idx,:)
A(idx,:)=[]

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by