Filter löschen
Filter löschen

How to save specified data from a matrix?

1 Ansicht (letzte 30 Tage)
Lei
Lei am 27 Aug. 2012
hello, everyone.
I am wondering how can I delete specified data from a data matrix. For example, if I have a matix like this:
m=rand(100,5);
How can I save a matrix that satisfy this : for each row, each element is smaller than a specified number such as 0.200.
I know I can use indice, eg. ind=find(m(:,2:5)< 0.2)
but I can't save it by using m1=m(ind,:)
I guess the problem is the number of rows is changing when I use m1=m(ind,:).
does anyone know how can i achieve this?
Thanks.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 27 Aug. 2012
Your line
ind=find(m(:,2:5)< 0.2)
is not correct. You need
ind=find(all(m(:,2:5)< 0.2,2))
  8 Kommentare
Lei
Lei am 27 Aug. 2012
Bearbeitet: Lei am 27 Aug. 2012
I see what you mean now!!! Thx a lot. But what if I want save the data meets this criteria: for each row, any of the element in this row is bigger than a number (0.2), then I save the data. Thats why I used this ind=find(m(:,2:5)< 0.2);. In this case, I cant save the data by using m1=m(ind,:);. What you code was doing is that all of the elements in that row are smaller than 0.2 Do you have any idea how can I do this???
Lei
Lei am 27 Aug. 2012
probaably ind=find(any(M(:,2:5)>54,2));

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Babak
Babak am 27 Aug. 2012
if your matrix m is 100 by 1, you can do this:
m(m<0.2)
gives you a new matrix that is smaller or equal size to the original matrix.
For a 100 by 5 m, I think you can write a for loop on the different columns of m,
a = m<.2;
for j=1:5
m(a(:,j)) % gives you the elemets of m(:,j) that are less than 0.2
end
  1 Kommentar
Lei
Lei am 27 Aug. 2012
hello Babak, Thx for your help,I tried your code. but it seems does not work.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements 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