Summing array elements

Hi everyone and Merry Christmas!
I'm trying to average some gps lat and long positions to smooth the output. the array is limited to two for testing purposes and would normally be in the order of 100.
The two lines fail to add correctly in the FOR loop and when I look at the values for smoothed_lat or smoothed_lon I get NaN. it seems the problem is adding the values. assigning an array element to another variable works as I have shown.
I know these are floating point numbers, am I expecting too much?
I know the values are correct in the array - position_array as can be seen.

Really struggling with this.

Cheers.
Paul.

image

// average the position to smooth any fluctuations
if (position_array.length == 2) {
    var junk = position_array.shift();
    position_array.push([float_lat, float_lon]);
} else {
    position_array.push([float_lat, float_lon]);
}

var smoothed_lat = 0;
var smoothed_lon = 0;

for (var loop = 0; loop <= position_array.length; loop++){
        smoothed_lat = smoothed_lat + position_array[(loop)[0]]; // This fails
        smoothed_lon = smoothed_lon + position_array[(loop)[1]];  // This fails
}

smoothed_lat =  position_array[0][0]; // This works

smoothed_lat = smoothed_lat / position_array.length; // gets the average
smoothed_lon = smoothed_lon / position_array.length; // gets the average

If the length is 2 that will run through the loop with indices 0, 1, and 2, so you are going off the end of the array. It should be

for (var loop = 0; loop < position_array.length; loop++){

Is the bit at the front with the shift and push in some way related to taking the average? I don't see what it is for.

Hi Colin,
The bit at the front is showing the array being filled and is not related to the averaging.

However thanks for your reply but it still fails with NaN. :frowning:

Cheers. Paul.

position_array[(loop)[0]]

I think that should be

position_array[loop][0]

If that doesn't fix it have a look at the node-red docs page Writing Functions which shows how you can use node.warn() to show values as the function is executed. That should enable you to work out what the problem is.

1 Like

Hi Colin, Yes that's cracked it!
Two silly mistakes.

Merry Christmas!

Cheers.
Paul.

The reduce method would work to
eg.

let smoothed = msg.payload.reduce( (accumulator, currentValue, index, array) => ([accumulator[0] + currentValue[0], accumulator[1] + currentValue[1]] ));
let length = msg.payload.length;
msg.smoothed = [(smoothed[0]/length), (smoothed[1]/length)];
return msg;

Here is an example flow

[{"id":"517fe8ab.2c4da","type":"function","z":"8d22ae29.7df6d","name":"","func":"let smoothed = msg.payload.reduce( (accumulator, currentValue, index, array) => ([accumulator[0] + currentValue[0], accumulator[1] + currentValue[1]] ));\nlet length = msg.payload.length;\nmsg.smoothed = [(smoothed[0]/length), (smoothed[1]/length)];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":2280,"wires":[["84c9d74a.048ca8"]]},{"id":"62276cee.8861cc","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[52.5673342345,1.2945556785],[51.3458652378,1.3456737106],[51.1234560076,1.1111234001]]","payloadType":"json","x":170,"y":2300,"wires":[["517fe8ab.2c4da"]]},{"id":"84c9d74a.048ca8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":500,"y":2280,"wires":[]}]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.