"Find" function with 3d matrix doesn't work

조회 수: 3 (최근 30일)
Wojtek
Wojtek 2012년 8월 21일
Hello, I have a 3d matrix named A3D (size 200x200x200). Values in this matrix are from 0 to 1. If I type for example:
A3D(50,92,100)
I get an answer:
ans =
1.0000
So I'm sure there are values 1 (actually I know it also from "image" of a slice of matrix - there I can see a lot of values "1"). But when I try to use "find":
[val1,val2,val3] = ind2sub(size(A3D),find(A3D == 1));
then vectors val1 val2 and val3 are empty. These vectors are filled when I type:
[val1,val2,val3] = ind2sub(size(A3D),find(A3D < 1));
It that case Matlab says that all of the values in my matrix are <1. But that is not true! Why matlab doesn't want to find values == 1? Thank you very much for your help!

채택된 답변

Titus Edelhofer
Titus Edelhofer 2012년 8월 21일
Hi,
the answer
ans =
1.0000
instead of
ans =
1
tells you the problem: the value is 1, but only rounded to 5 digits. It's not exactly one. Change your code to
[val1,val2,val3] = ind2sub(size(A3D),find(abs(A3D-1)<1e-10));
where 1e-10 is the tolerance. Choose the tolerance in such a way that you only catch those entries that are 1 (within the tolerance).
Titus
  댓글 수: 4
Matt Fig
Matt Fig 2012년 8월 21일
편집: Matt Fig 2012년 8월 21일
If that is all you are doing, why even bother with FIND and IND2SUB?
A3D(abs(A3D-1)<1e-10) = 0;
In fact, what you are trying to do above will not even work. Look at a simpler example so you can see for yourself.
A = rand(3,3,3)
B = A; % A copy, to compare.
[I,J,K] = ind2sub(size(A),find(abs(A-1)<.2)) % Find all on [.8 1];
A(I,J,K) = nan % Does this look right to you?? NO!
B(abs(B-1)<.2) = nan % That looks better!!
Wojtek
Wojtek 2012년 8월 21일
편집: Wojtek 2012년 8월 21일
I get it now. I wanted to change values in this matrix (for example values == 1) with values from another matrix but with the same coordinates. That's why I wanted to save coordinates from the 1st matrix to apply them to the 2nd one. But of course now I can do:
A3D(abs(A3D-1) < 1e-4) = B(abs(A3D-1) < 1e-4);
Well, thanks a lot! I would wait for a long time, because matlab didn't show any error - it just went "busy". Thanks once again!

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 21일
use
[val1,val2,val3] = ind2sub(size(A3D),find(A3D <= 1 & A3D>1-eps));
maybe 1 is 0.9999999999999999999 for numeric considerations
  댓글 수: 2
Jan
Jan 2012년 8월 21일
편집: Jan 2012년 8월 21일
There are not much numbers, which can be represented by a IEEE754 double value between 1-eps and 1... Think of the definition of EPS.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 21일
you are right; maby 1-10^(-10) can work

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by