how to refine data?

6 views (last 30 days)
Darkhan Kenestegi
Darkhan Kenestegi on 27 Dec 2016
Edited: KSSV on 27 Dec 2016
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 Comments
KSSV
KSSV on 27 Dec 2016
Edited: KSSV on 27 Dec 2016
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 on 27 Dec 2016
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!

Sign in to comment.

Accepted Answer

KSSV
KSSV on 27 Dec 2016
Edited: KSSV on 27 Dec 2016
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

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!