how to change NaN values using ratio
1 view (last 30 days)
Show older comments
Hello dear all,
I have a problem to solve my data on matlab.
There are many time series units such as [778.2, 778.2, 778.3, NaN, NaN, NaN, 778.6, 778.7, 778.8, NaN, 778.8,...]
It's exactly a cumulative data getting an increased.
So, I can calculate each differences are back - forth for each values [0.0000, 0.1000, NaN, NaN, NaN, NaN, 0.1000, 0.1000, NaN, NaN, ...]
but I wanna change that 'NaN' values using another ratio.
for example, ratio = [0.0167, 0.0224, 0.0129 ,0.0102 ,0.0109 ,0.0116 ,0.0201, 0.0578, 0.0750, 0.0528,...]
we can obtain differences from known values as 778.6 - 778.3 = 0.3 , 778.8 - 778.8 = 0
first NaN is 0.3*(0.0102/(0.0129+0.0102+0.0109+0.0116))+ 778.3 = 778.3668511
second NaN is 0.3*(0.0116/(0.0129+0.0102+0.0109+0.0116))+ 778.3 = 778.3763248
third NaN is 0.3*(0.0109/(0.0129+0.0102+0.0109+0.0116))+ 778.3
forth NaN is 0*(0.0528/(0.0750+0.0528))+778.8 = 778.8
I tried many things using diff(), but I cannot find a simple way to get that result.
If anyone can help, it would be greatly appreciated.
Thank you!
0 Comments
Answers (2)
John D'Errico
on 3 Sep 2018
Edited: John D'Errico
on 3 Sep 2018
Be clear about your goal.
vec = 1:12;
vec([3 4 5 7 8 11]) = NaN
vec =
1 2 NaN NaN NaN 6 NaN NaN 9 10 NaN 12
Do you want to find ANY NaN?
find(isnan(vec))
ans =
3 4 5 7 8 11
Do you want to find all pairs of consecutive NaNs? This will work, finding the first of each pair of consecutive NaNs.
nanlocs = strfind(isnan(vec),[1 1])
nanlocs =
3 4 7
All strings of NaNs, of any length? So, this will find the beginning of any sequence of NaNs, even as short as length 1.
nanlocs = strfind([0,isnan(vec)],[0 1])
nanlocs =
3 7 11
If you want to exclude the singleton NaNs, then you could do this:
nanlocs = setdiff(strfind([0,isnan(vec)],[0 1]),strfind([0,isnan(vec)],[0 1 0]))
nanlocs =
3 7
Lots of other ways, I'm sure. And it will be easy enough to find the length of those sequences, or the endpoints. (Hint: consider what I did above. How might you modify it to find the endpoint of such a sequence of NaNs? What would that tell you about the length?)
Tiasa Ghosh
on 3 Sep 2018
if
A=[778.2; 778.2; 778.3; NaN; NaN; NaN; 778.6; 778.7; 778.8; NaN; 778.8]
then maybe you could search for all the rows containing NaN by
find(any(A==NaN,2))
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!