Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
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
I wrote a program to remove NaN ,, but it does nothing (page 3) in avg sheet
my goel is to find the averag without the nans >> because in my original sheet all the average valuse are shown as NaN
this is on page 22
Stuart >> I'm using the mean function you showed me in this thread:
Solved: Re: average value - PTC Community
Solved! Go to Solution.
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:
Maybe this alternative mean function could be of help
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
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
But then you wrote
and I had no idea where the value 0.444 would stem from and how it would be derived from the given values ???
it didn't work
I tried to write another way
it didn't work as well
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
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.
Is there a way to replace NaN with zeros
I tried to use this
but it does not work still showing NaNs
I tried this one as well but it doesn't change anything
All I want is to remove NaN or replace it with zero
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.
But of course it does not work for deeply nested vectors because vectorization works just one level deep and not recursively.
You will have to loop through your data structure yourself!
Either down to the last vector using vectorization
or with an additional loop down to the last scalar
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:
The only way in Prime to check for NaN's is to use the function IsNaN, as demonstrated by:
Define an alternative function
Check use:
Correct, since 1 is not a NaN.
Incorrect, since NaN is a NaN.
So to check for NaN's use:
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:
and to replace NaNs by Zero's:
These function will not work for nested vectors, though.
Success!
Luc
I see ... so I should use IsNaN to be able to replace the NaNs by zeros>>> Thanks Luc
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
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 😉
Thanks
Yusra
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
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.
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
@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.
It helps ...thank you my friend