cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

FilterNaN

YA_10963798
14-Alexandrite

FilterNaN

Hi there ,

 

I want to remove NaN from a nested matrix so I can find the average . I tried to use (filterNaN) function but it shows this error 

YA_10963798_0-1734942525056.png

I wrote a program to remove NaN ,, but it does nothing (page 3) in avg sheet

YA_10963798_1-1734942686574.png

my goel is to find the averag without the nans >> because in my original sheet all the average valuse are  shown as NaN 

YA_10963798_2-1734942762277.png

this is on page 22

@StuartBruff 

Stuart >> I'm using the mean function you showed me in this thread:

Solved: Re: average value - PTC Community

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Werner_E)

Here is a more generic function which should be able to deal with nested arrays of any structure and should hopefully also add the appropriate unit. As it works recursively no vectorization should be necessary:

Werner_E_0-1734962002409.png

 

View solution in original post

13 REPLIES 13
Werner_E
25-Diamond I
(To:YA_10963798)

Maybe this alternative mean function could be of help

B.png

 

filterNaN will throw an error if no NaN is found (or if all entries are NaN.

This also applies if you use filterNaN vectorized on a a nested vector.

You may try this alternative function which tried to catch this error - it vas written with vectors (one column matrices) in mind

B2.png

 

According your "YA avg 02.mcdx" file: Is there anywhere in that file the information what exactly you are trying to achieve?? I mean is there anywhere the manually typed in nested vector you want to see as the result of your attempts?

It looks to me like the nested vector n should be the input of your calculations, but its unclear to me, what the output should be.

At first I thought you might be after something like

Werner_E_0-1734954628261.png

 

But then you wrote

Werner_E_1-1734954738890.png

and I had no idea where the value 0.444 would stem from and how it would be derived from the given values ???

 

 

YA_10963798
14-Alexandrite
(To:Werner_E)

it didn't work

YA_10963798_0-1734953500036.png

I tried to write another way

YA_10963798_1-1734953532724.png

it didn't work as well

 

Werner_E
25-Diamond I
(To:YA_10963798)

mean2(), like the original mean() is supposed to work on a vector of scalars, not a nested vector of nested vectors of nested....

The same applies to the deleteNaN function shown above.

Only difference from mean2 and mean is that mean2 properly handles NaNs without throwing an error and that mean2 was written specifically to take one vector argument.

You can use both functions vectorized, though.

But if you use

Werner_E_0-1734955601485.png

you will just get the mean values of the three vectors in n and this seems to be not what you are looking for.

But mean or mean2 will only return the mean value of a vector and not the mean value of a couple of values picked from various vectors in the way you may have in mind - how should Prime know which values you want to play with if you don't collect them in a vector yourself?

I, too am not sure which result with the nested vector n as input you expect to see. See my added remarks above.

YA_10963798
14-Alexandrite
(To:Werner_E)

Is there a way to replace NaN with zeros 

I tried to use this 

YA_10963798_0-1734956132577.png

but it does not work still showing NaNs 

I tried this one as well but it doesn't change anything 

YA_10963798_1-1734956252504.png

 

 

All I want is to remove NaN or replace it with zero 

Werner_E
25-Diamond I
(To:YA_10963798)

This does not explain the value 0.444 I asked for in a previous answer and also does not explain which exact result you expect with an input of nested vector n.

 

BTW, its a huge difference if you simply ignore the NaN's when calculating the averages or if you replace them by zeros!!
If you use filterNaN on a vector and then take the average you are just dividing the sum by the number of non-NaN values where otherwise you still divide by the total number of vector entries.

 

Here is a function to replace NaNs by zeros (why don't you assign zeros right ahead if this what is appropriate for your work?).

It also works for a vector of scalars if you apply vectorization.

Werner_E_0-1734960567817.png

But of course it does not work for deeply nested vectors because vectorization works just one level deep and not recursively.

Werner_E_1-1734960600535.png

You will have to loop through your data structure yourself!

Either down to the last vector using vectorization

Werner_E_2-1734960723177.png

or with an additional loop down to the last scalar

Werner_E_3-1734960735417.png

 

Werner_E
25-Diamond I
(To:Werner_E)

Here is a more generic function which should be able to deal with nested arrays of any structure and should hopefully also add the appropriate unit. As it works recursively no vectorization should be necessary:

Werner_E_0-1734962002409.png

 

LucMeekes
23-Emerald III
(To:YA_10963798)

The only way in Prime to check for NaN's is to use the function IsNaN, as demonstrated by:

Define an alternative function
LucMeekes_0-1734960247970.png

Check use:

LucMeekes_1-1734960302381.png

Correct, since 1 is not a NaN.

LucMeekes_2-1734960327767.png

Incorrect, since NaN is  a NaN.

So to check for NaN's use:

LucMeekes_3-1734960390915.png

Your function replaceZerosByNaNs2(x,a,b) replaces values of x by value b only when x equals a, and because it doesn't use the IsNaN function it cannot check for NaNs. Besides the fact that with your call:

replaceZerosByNaNs2(Rt;k,NaN,0)

it would do the opposite of what the function name tells: replace NaNs by zero's, if it worked.

To replace Zero's by NaNs you could use:

LucMeekes_4-1734960858731.png

and to replace NaNs by Zero's:

LucMeekes_5-1734960889757.png

These function will not work for nested vectors, though.

 

Success!

Luc

 

 

 

YA_10963798
14-Alexandrite
(To:LucMeekes)

I see ... so I should use IsNaN to be able to replace the NaNs by zeros>>> Thanks Luc 

YA_10963798
14-Alexandrite
(To:Werner_E)

Hi Werner 

for your question:

<This does not explain the value 0.444 > I think we have played in that sheet so much .. me neither I don't know from where 0.444 . came . 

 

If you still remember how we  got the NaNs >> from this program 

YA_10963798_0-1734965702863.png

from this function which means these NaNs should be excluded , because they don't exist >> so I don't want to include them in the average calculation

The last solution works ,but does it count the NaNs in the average or no? ... I hope it doesn't 😉

YA_10963798_1-1734965908613.png

 

 

Thanks 

Yusra 

Werner_E
25-Diamond I
(To:YA_10963798)

NaN2Zero2 just replaces all NaN's with zeros, trying to even ad a correct unit.

It has nothing to do with average values.

It was just my answer to your question "Is there a way to replace NaN with zeros ?".

 

If you take the mean of the vectors thus created, the NaN's sure are counted as 0.

If you don't want the NaNs to be counted, you should remove them (via trimNaN) and take the mean of the resulting vector. Use try..on error .. to catch the two possible error states (no NaN at all or all NaN's) and in case of nested vectors loop through them as was shown above.

This is what my remark about ignoring the NaN's versus replacing them by 0 was all about.

You ave to decide what you need!

1) if the vector is (3; 3; NaN)^T and you ignore the NaN, then I would say that the average is 3 ((3+3)/2.

2) If you replace the NaN by 0, the average is just 2 ((3+3+0)/3).

 

If option 1) is what you are looking for, then the answer was given in form of the function "avg" in the sheet I posted

Werner_E_0-1734970664606.png

This routine also takes one element of each vector and then calculated the mean value of them as this is what I first thought is what you are looking for. Later I saw the strange value 0.444 and was confused. You explained that you don't know either about this value but you still did not explain what result(s) you expects and if the vector I showed here [example] was the one you are looking for.

 

If option 2) is the desired one (after all YOU wanted the NaN's replaced by zeros), then you can use one of the ways to replace NaNs by zeros and then use the built-in mean for each vector you want the average to be calculated.

 

Again - provide an example or two for the input (as simplified as possible) and manually type in the exact result you expect and explain how to arrive at those value in case its not obvious.

YA_10963798
14-Alexandrite
(To:Werner_E)

Hi Werner 

 

I think option one is fine for me . Thank you for the solution

I know I always bother you with long sheets >> believe me I do my best to simplify the sheet , but when I don't know where is the problem how can I write about it ;( .. HHH sorry bro for that , but I notice my mathcad skills are away better .. you have been teaching me almost for a year < Thank you> 

 

Yusra 

Werner_E
25-Diamond I
(To:YA_10963798)


@YA_10963798 wrote:

Hi Werner 

 

I think option one is fine for me . Thank you for the solution

I know I always bother you with long sheets


I just only looked at your shorter "YA avg 02.mcdx" and tried to find out what may be needed and so I came up with "avg" and hope that it helps.

YA_10963798
14-Alexandrite
(To:Werner_E)

It helps ...thank you my friend

Announcements

Top Tags