how to refine data?

조회 수: 1 (최근 30일)
Darkhan Kenestegi
Darkhan Kenestegi 2016년 12월 27일
편집: KSSV 2016년 12월 27일
Let's say I have a data matrix as:
a=[1,1,1,1,1;1,0,1,1,0;0,0,0,1,1;1,0,1,0,1];
Now, what I want is to get rid of the zeroes, yet not messing up my matrix. As of I need values stay at their corresponding rows. Reason I want it is due to my original data having 0 and I need to calculate log of each values in the row. Ln(0) simply is messing my code for obvious reason.
Will really appreciate the help!
  댓글 수: 2
KSSV
KSSV 2016년 12월 27일
편집: KSSV 2016년 12월 27일
You cannot get rid of zeros without disturbing the dimensions. The option would be to replace zeros with some other number. Is it okay to replace with nan?
Also not that log(1) is 0.
Darkhan Kenestegi
Darkhan Kenestegi 2016년 12월 27일
Thanks for the answer, yeah I know, probably I used a bad example, just created a matrix out of my head really. My data is just quite big and checking each row individually is just not an option.
I get that it won't be possible without disturbing the dimensions, makes sense.
Actually later in the code I do get NaN instead of zeroes in the matrix. Are you suggesting that I can code it in such a way that I can neglect those NaN values and concentrate on real values? But the question then switches on how to get rid of NaN?
Thanks for your support!

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

채택된 답변

KSSV
KSSV 2016년 12월 27일
편집: KSSV 2016년 12월 27일
How about this?
a=[ 0.2077 0.2305 0.8443 0.1707 0.3111
0.3012 0 0.1948 0.2277 0
0 0 0 0.4357 0.9234
0.4709 0 0.2259 0 0.4302];
idx = a~=0 ;
%%apply log
log_a = a;
log_a(idx) = log(a(idx)) ;
log_a

추가 답변 (0개)

카테고리

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