double array of 0's and 1's conversion help

조회 수: 2 (최근 30일)
David C
David C 2012년 6월 20일
I have an array of 0's and 1's
Im trying to convert the 0's to nans and 1's to another value.
A(find(A==1)=256; %this part works
A(find(A==0)=NaN; %this part doesn't
when i try to replace the 0's, it replaces everything in the array with NaN, even though find(A==0) does return only the indices of where that array has a 0 value.
is there technical thing I'm missing here?

답변 (2개)

Walter Roberson
Walter Roberson 2012년 6월 20일
A(A==1) = 256;
A(A==0) = NaN;
If you want to live a life of confusion, and you only have 0 and 1s in the matrix,
A = A ./ A * 256;
  댓글 수: 4
Walter Roberson
Walter Roberson 2012년 6월 20일
Reduces the steps that can go wrong ?
David C
David C 2012년 6월 20일
sorry, i just forgot to put the brackets in while typing, i have them in my actual code

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


David C
David C 2012년 6월 20일
just tried
A(A==1) = 256;
A(A==0) = NaN;
and the same thing happened. The A==1 part worked, but as soon as it tried to logical index the ones that were 0, it replaced everything with NaN
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2012년 6월 20일
What happens if you run this:
A = double(rand(10)>0.5);
A(A==1) = 256;
A(A==0) = NaN;
A
Walter Roberson
Walter Roberson 2012년 6월 20일
Try breaking it down and do some experiments to see which step is going wrong:
A
T = A == 0
B = A;
A(T) = NaN
B(1) = NaN
C = rand(size(A));
C(T) = NaN

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by