How to save specified data from a matrix?

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.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 8월 27일

0 개 추천

Your line
ind=find(m(:,2:5)< 0.2)
is not correct. You need
ind=find(all(m(:,2:5)< 0.2,2))

댓글 수: 8

Lei
Lei 2012년 8월 27일
hello Walter, Thx, I see. But how can I save the data if it satisfy this: ind=find((m(:,2:5)< 0.2)) when I use m2=m(ind,:); there's an error ??? Index exceeds matrix dimensions.
Walter Roberson
Walter Roberson 2012년 8월 27일
m(:,2:5)< 0.2 returns a two-dimensional matrix, so find() on it is going to return the linear indices relative to the two-dimensional matrix. You then try to use those linear indices as if they were column indices.
Your question asked "for each row, each element is smaller than a specified number such as 0.200" which is only true if all() of the comparisons are true along the row. When all() is used specifying row processing, the number of logical values produced would be the same as the number of rows, and find() applied to that is going to give row indices, which would be fine to pass in to the first index of m. Thus my answer find(all(m(:,2:5)< 0.2,2)) which includes the "all" step.
Lei
Lei 2012년 8월 27일
Hello, Walter. I see what you mean! My problem is no matter whether I use ind=find((m(:,2:5)< 0.2)) or find(all(m(:,2:5)< 0.2,2)), I do not know how to save this data in the workspace. ind=find(whatever) is to extract the data I need to save ( I just used a simple example,but your suggests are absolutely right). Now the problem is that I do not know how to save it!
As you had above,
m1=m(ind,:);
and then save m1
Lei
Lei 2012년 8월 27일
편집: Walter Roberson 2012년 8월 27일
hello, Walter,
This one (m1=m(ind,:);) does not work! As I mentioned above.
try to tun this
m=rand(100,5);
ind=find(m(:,2:5)< 0.2);
m1=m(ind,:);
The first two lines are ok, but the third line has an error!
it says '??? Index exceeds matrix dimensions.'
Thats why I am asking for help here!
any other suggestions? thx a lot
m=rand(100,5);
ind=find(all(m(:,2:5)< 0.2,2))
m1=m(ind,:);
Do not omit the all() !
Lei
Lei 2012년 8월 27일
편집: Lei 2012년 8월 27일
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 2012년 8월 27일
probaably ind=find(any(M(:,2:5)>54,2));

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Babak
Babak 2012년 8월 27일

0 개 추천

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

Lei
Lei 2012년 8월 27일
hello Babak, Thx for your help,I tried your code. but it seems does not work.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

Lei
2012년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by