Max value of each element of an array

88 views
Skip to first unread message

kartheek koka

unread,
Jul 23, 2017, 8:44:35 PM7/23/17
to Node-RED

Hi, I am sending array of 50 elements to a node every 10 mins.


0th Min: a[0],a[1],a[2],.......a[49]
10th Min: b [0],b[1],b[2],.........b[49]
20th Min: c[0],c[1],c[3],............c[49]



I want the max of a[0],b[0] and c[0] in Array max[0]
I want max of a[1],b[1] and c[1] in Array max[1]


Can i use the Smooth function for this. can any one help me please. Below is my code. 


[{"id":"ad6c767c.c1c8c8","type":"function","z":"e8c788d.eb2c978","name":"Motor curr var","func":"var valuesnew = new Int16Array(msg.payload);\nvar ui16=new Array(0);\n//adding a number to the first element and shifting the rest\nui16[0]=33000;\nfor (var i=1;i<1+valuesnew.length;i=i+1){\n   ui16[i]=valuesnew[i-1];\n}\nvar i=1;\n\n\nmsg.payload={ui16};\n\n\n//var msg ={payload:i,'max':context.global.max,'min':context.global.min,'avg':context.global.avg};\n//var msg2= { payload:b,'max':b,'min':c,'avg':d };\nreturn [msg];","outputs":"1","noerr":0,"x":1250.9355239868164,"y":1330.2640323638916,"wires":[["c738e86c.8858c8","59460816.95e988"]]},{"id":"59460816.95e988","type":"debug","z":"e8c788d.eb2c978","name":"","active":true,"console":"false","complete":"false","x":1458.0089416503906,"y":1436.250108242035,"wires":[]},{"id":"c7d155a9.046eb8","type":"debug","z":"e8c788d.eb2c978","name":"","active":true,"console":"false","complete":"true","x":1583.2973747253418,"y":1313.2503566741943,"wires":[]},{"id":"c738e86c.8858c8","type":"function","z":"e8c788d.eb2c978","name":"","func":"var valuesnew = new Int16Array(msg.payload.ui16);\nvar msg1;\n\nvar j=context.global.qtyofspc;\n\n\n    context.global.max[i]=Math.max(valuesnew[i],context.global.max[i]);\n    context.global.min[i]=Math.min(valuesnew[i],context.global.min[i]);\n    context.global.avg[i]= (valuesnew[i]+context.global.avg[i])/2;\n\nmsg.payload={'max':context.global.max[0],'ui16':valuesnew,'min':context.global.min,'avg':context.global.avg};\n\nmsg1 = { payload:valuesnew };\n\nreturn [msg];","outputs":1,"noerr":0,"x":1430.433708190918,"y":1313.5336933135986,"wires":[["c7d155a9.046eb8"]]}]



Mark Setrem

unread,
Jul 24, 2017, 2:19:48 AM7/24/17
to Node-RED
if you want to store data between messages you will either need to put it in a database or store it using the context object.
This page will show you how https://nodered.org/docs/writing-functions#storing-data

kartheek koka

unread,
Jul 24, 2017, 3:29:24 AM7/24/17
to Node-RED
Hi Mark, 

 i tried it. here is my code. For the second count all my min,max and avg are having the same value as my avg array. Can you please check my code. I have been banging my head for past two days on this. 


var ui16 = new Int16Array(msg.payload);

var u16=new Array(0);
var count = context.get('count')||0;
count =count+ 1;
// store the value back
context.set('count',count);
// make it part of the outgoing msg object


for (var i=0;i<ui16.length;i=i+1)
{
    u16[i]=ui16[i];
}


if (count == 1)

{
var max= new Array(0);
var min= new Array(0);
var avg=new Array(0);

  
    context.set('max',u16);
     context.set('min',u16);
      context.set('avg',u16);
 
}

else{
    var a=new Array(0);
    var b=new Array(0);
    var c= new Array(0);
    a=context.get('max');
    b=context.get('min');
    c=context.get('avg');
for (var i=0;i<u16.length;i=i+1)
{
  a[i]=Math.max(a[i],u16[i]);
  b[i]=Math.min(b[i],u16[i]); 
 c[i]=((c[i]+u16[i])/3);
    
}
context.set('max',a);
context.set('min',b);
context.set('avg',c);
    
}


msg1={payload:count,'input':u16,'max':context.get('max'),'min':context.get('min'),'avg':context.get('avg'),'filename':"motcurr"};

 
return [msg1];

Mark Setrem

unread,
Jul 24, 2017, 3:52:59 AM7/24/17
to Node-RED
OK take a step back.  

Rather than trying to complete the assignment in one go, start by taking one of the things you wish to analyse and start by determining that.

This will allow you to simplify your code and make it easier to see where you have been going wrong.  
It might also help you if you add comments to your code explaining what you think each line does. 


kartheek koka

unread,
Jul 24, 2017, 1:32:49 PM7/24/17
to Node-RED
ok. so here is it for getting the max values. i am able to get the result for this. But when i add for getting min values, I get the same values in u16,max and min

//storing the input in ui16
var ui16 = new Int16Array(msg.payload);

var u16=new Array(0);
//to count number of times this function is executing
var count = context.get('count')||0;
count =count+ 1;
// store the value back
context.set('count',count);
// make it part of the outgoing msg object

//copying the ui16 in u16
for (var i=0;i<ui16.length;i=i+1)
{
    u16[i]=ui16[i];
}

//first array "max" is empty. So, just copy u16 in max
if (count == 1)

{
var max= new Array(0);

    context.set('max',u16);
   
}

else{
    
    var a=new Array(0);
  
    a=context.get('max');
 
 //compare u16 with a and put max values into a
for (var i=0;i<u16.length;i=i+1)
{
  a[i]=Math.max(a[i],u16[i]);

}


//copy values of a in max
context.set('max',a);

}


// initialise the counter to 0 if it doesn't exist already

msg1={payload:count,'input':u16,'max':context.get('max'),'min':context.get('min'),'filename':"motcurr"};
   
    

return [msg1];
Reply all
Reply to author
Forward
0 new messages