Calculate min/avg/max/std-dev for ICMP time stamp data from hping [closed] - perl

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What's the best way to calculate min/avg/max/std-dev for some random data in shell?
What if one has several columns per line, and needs to calculate the statistics for each one?
Sample input (based on processing of the hping output), with the columns 3, 4 and 5 being of interest:
0 145.5 146 = 75 + 71
1 142.7 142 = 72 + 70
2 140.7 140 = 70 + 70
3 146.7 146 = 76 + 70
4 148.3 148 = 77 + 71
5 157.5 157 = 87 + 70
6 167.1 167 = 96 + 71
7 166.3 166 = 95 + 71
8 167.7 167 = 97 + 70
9 159.0 159 = 88 + 71
10 156.7 156 = 86 + 70
11 154.9 155 = 84 + 71
12 151.9 152 = 81 + 71
13 157.3 157 = 86 + 71
14 155.0 155 = 84 + 71
15 157.7 158 = 87 + 71
16 156.6 156 = 86 + 70
(Note that this input is a live stream ad infinitum.)

I suggest you use Perl and keep a running total of N, Σx, and Σx², as well as the minimum and maximum x values. All of the values you need can be derived from those.
This example demonstrates. It dumps the current statistics after each line of the input is read.
use strict;
use warnings;
my ($n, #sum, #sumsq, #min, #max);
while (<DATA>) {
my #columns = /[0-9.]+/g;
my (#mean, #std_dev);
++$n;
for my $i (0 .. 2) {
my $x = $columns[$i + 2];
my $xsq = $x * $x;
$sum[$i] += $x;
$sumsq[$i] += $xsq;
$mean[$i] = $sum[$i] / $n;
$std_dev[$i] = sqrt($sumsq[$i]/$n - $mean[$i] * $mean[$i]);
$min[$i] = $x unless defined $min[$i] and $min[$i] <= $x;
$max[$i] = $x unless defined $max[$i] and $max[$i] >= $x;
}
print "min = #min\n";
print "max = #max\n";
print "mean = #mean\n";
print "std_dev = #std_dev\n";
print "---\n";
}
__DATA__
0 145.5 146 = 75 + 71
1 142.7 142 = 72 + 70
2 140.7 140 = 70 + 70
3 146.7 146 = 76 + 70
4 148.3 148 = 77 + 71
5 157.5 157 = 87 + 70
6 167.1 167 = 96 + 71
7 166.3 166 = 95 + 71
8 167.7 167 = 97 + 70
9 159.0 159 = 88 + 71
10 156.7 156 = 86 + 70
11 154.9 155 = 84 + 71
12 151.9 152 = 81 + 71
13 157.3 157 = 86 + 71
14 155.0 155 = 84 + 71
15 157.7 158 = 87 + 71
16 156.6 156 = 86 + 70
output
min = 146 75 71
max = 146 75 71
mean = 146 75 71
std_dev = 0 0 0
---
min = 142 72 70
max = 146 75 71
mean = 144 73.5 70.5
std_dev = 2 1.5 0.5
---
min = 140 70 70
max = 146 75 71
mean = 142.666666666667 72.3333333333333 70.3333333333333
std_dev = 2.4944382578501 2.05480466765642 0.47140452079146
---
min = 140 70 70
max = 146 76 71
mean = 143.5 73.25 70.25
std_dev = 2.59807621135332 2.38484800354236 0.433012701892219
---
min = 140 70 70
max = 148 77 71
mean = 144.4 74 70.4
std_dev = 2.93938769133971 2.60768096208109 0.489897948555485
---
min = 140 70 70
max = 157 87 71
mean = 146.5 76.1666666666667 70.3333333333333
std_dev = 5.40832691319598 5.39804491356711 0.47140452079146
---
min = 140 70 70
max = 167 96 71
mean = 149.428571428571 79 70.4285714285714
std_dev = 8.74817765279739 8.55235974119756 0.494871659305337
---
min = 140 70 70
max = 167 96 71
mean = 151.5 81 70.5
std_dev = 9.8488578017961 9.59166304662544 0.5
---
min = 140 70 70
max = 167 97 71
mean = 153.222222222222 82.7777777777778 70.4444444444444
std_dev = 10.4857339888036 10.3470637571759 0.496903995000609
---
min = 140 70 70
max = 167 97 71
mean = 153.8 83.3 70.5
std_dev = 10.0975244490914 9.94032192637645 0.5
---
min = 140 70 70
max = 167 97 71
mean = 154 83.5454545454545 70.4545454545455
std_dev = 9.64836302648838 9.50945592902742 0.497929597732158
---
min = 140 70 70
max = 167 97 71
mean = 154.083333333333 83.5833333333333 70.5
std_dev = 9.24173805202349 9.10547759440561 0.5
---
min = 140 70 70
max = 167 97 71
mean = 153.923076923077 83.3846153846154 70.5384615384615
std_dev = 8.89651218141581 8.77530154238378 0.498518515262866
---
min = 140 70 70
max = 167 97 71
mean = 154.142857142857 83.5714285714286 70.5714285714286
std_dev = 8.60943952761114 8.48287590817347 0.494871659305337
---
min = 140 70 70
max = 167 97 71
mean = 154.2 83.6 70.6
std_dev = 8.32025640630559 8.19593395125498 0.489897948558269
---
min = 140 70 70
max = 167 97 71
mean = 154.4375 83.8125 70.625
std_dev = 8.10839649684202 7.97824189593171 0.484122918275927
---
min = 140 70 70
max = 167 97 71
mean = 154.529411764706 83.9411764705882 70.5882352941177
std_dev = 7.874886718579 7.75712642546343 0.492152956783766
---

Related

Matlab lego nxt robot doesn’t turn but says it did

I am programming a Lego brick NXT in Matlab to go through a maze. The robot is supposed to drive forward until the touch sensor is activated by a wall.
The robot should
stop
backup
stop
read the ultraSonic (distance) sensor
display the result
If the sensor reads a large distance, the robot turns right and stops and if the sensor reads a small distance, the robot turns left and stops.
Then, the robot continues forward again until it runs into another wall.
Current code:
The robot will hit the wall, stop, back up, stop, and it will display that it’s turning, but it doesn’t turn.
Is there something I am typing incorrectly? Should I initialize the motors before the loop or inside of the loop? I am especially wary of my pauses.
1 %B = NXTMotor('B');
2 %C = NXTMotor('C');
3 OpenSwitch(SENSOR_1);
4 OpenUltrasonic(SENSOR_4);
5
6
7 while 1
8
9 B = NXTMotor('B');
10 C = NXTMotor('C');
11
12 %FORWARD 1
13 disp('move forward');
14 B.Power = 50;
15 C.Power = 50;
16
17 B.SendToNXT();
18 C.SendToNXT();
19 B.WaitFor();
20 C.WaitFor();
21 disp('before getswitch');
22
23 if GetSwitch(SENSOR_1)
24 pause(0.2);
25 B.Power = 0;
26 C.Power = 0;
27 B.SendToNXT();
28 C.SendToNXT();
29 B.WaitFor();
30 C.WaitFor();
31 disp('Hit a wall and stops');
32
33 disp('back up');
34 B.Power = -50;
35 C.Power = -50;
36
37 B.TachoLimit = 720;
38 C.TachoLimit = 720;
39
40 B.SendToNXT();
41 C.SendToNXT();
42 B.WaitFor();
43 C.WaitFor();
44
45 pause(0.5);
46 B.Power = 0;
47 C.Power = 0;
48 B.SendToNXT();
49 C.SendToNXT();
50 B.WaitFor();
51 C.WaitFor();
52 disp('finished back up');
53
54 %ultrasonic
55 distance = GetUltrasonic(SENSOR_4);
56 disp(distance);
57
58 if distance >100
59 %if GetUltrasonic(SENSOR_4) > 100
60
61 %TURN 1
62 disp('turn1 > 100');
63
64 B.Power = 0;
65 C.Power = 100;
66
67 B.TachoLimit = 720;
68 C.TachoLimit = 720;
69
70 B.SendToNXT();
71 C.SendToNXT();
72 B.WaitFor();
73 C.WaitFor();
74 disp('turn1 finished');
75
76 pause(0.2);
77 B.Power = 0;
78 C.Power = 0;
79 B.SendToNXT();
80 C.SendToNXT();
81 B.WaitFor();
82 C.WaitFor();
83
84 end
85
86 if distance <= 100
87
88 disp('turn2 < 100');
89
90 B.Power = 100;
91 C.Power = 0;
92 disp('turn 2');
93 B.TachoLimit = 720;
94 C.TachoLimit = 720;
95
96 B.SendToNXT();
97 C.SendToNXT();
98 B.WaitFor();
99 C.WaitFor();
100 disp('Finished turn 2');
101
102 pause(0.5);
103 B.Power = 0;
104 C.Power = 0;
105 B.SendToNXT();
106 C.SendToNXT();
107 B.WaitFor();
108 C.WaitFor();
109
110 disp('stopped turn2 and ready to go forward');
111
112 end
113
114 B.Stop('off');
115 C.Stop('off');
116 end
117
118 %B.Stop('off');
119 %C.Stop('off');
120 end
121
122 CloseSensor(SENSOR_4);
123 CloseSensor(SENSOR_1);

MRT function [1] "error code = 0"

When I make my MRT, I got two errors:
[1] "error code = 0" and Error in indval.default(Ynode, clustering =
clustnode, numitr = 1000) : All species must occur in at least one
plot. Does anyone have an idea of why? I checked and all my species
have an abundance >0...
MRTtest=mvpart(vegetation~ Placette+ Tourb + Transect + Largcanal + Annouvert + Elevation + Profnappe + Litiere+ Solnu+ Deblign+ Densiometre+ EpaissMO+ Vonpostvingt+ Vonpostsoixante+ Pyrovingt+ Pyrosoixante+ Sommesurfterr,tot,margin=0.08,cp=0,xv="pick",xval=10,xvmult=150,which=4,pca=F)
X-Val rep : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129 130 131 132 133
134 135 136 137 138 139 140 141 142 143 144 145 146 147
148 149 150
Minimum tree sizes
tabmins
2 3 4 6
2 125 5 18
MRTtest1=MRT(MRTtest,percent=10,species=colnames(vegetation))
summary(MRTtest1)
Portion (%) of deviance explained by species for every particular node
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- Node 1 ---
Complexity(R2) 14.87422
Sommesurfterr>=6.024 Sommesurfterr< 6.024
~ Discriminant species :
THOnmtot THOmtot
% of expl. deviance 17.61057298 38.419650816
Mean on the left 0.37621604 0.430818462
Mean on the right 0.08877576 0.006259911
[1] "error code = 0"
~ INDVAL species for this node: : left is 1, right is 2
cluster indicator_value probability
THOmtot 1 0.9597 0.001
THOnmtot 1 0.7878 0.001
LEG 1 0.5802 0.031
LIB 1 0.5078 0.010
MELnmtot 1 0.4710 0.047
EPNnmtot 1 0.4404 0.026
Sum of probabilities = 87.497
Sum of Indicator Values = 30.02
Sum of Significant Indicator Values = 12.67
Number of Significant Indicators = 29
Significant Indicator Distribution
1 2
8 21
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- Node 2 ---
Complexity(R2) 7.920283
Densiometre< 19.88 Densiometre>=19.88
~ Discriminant species :
TRA THOmtot
% of expl. deviance 10.54536819 27.8848051
Mean on the left 0.02754503 0.5158733
Mean on the right 0.20823621 0.2220475
Error in indval.default(Ynode, clustering = clustnode, numitr = 1000)
: All species must occur in at least one plot

Error in for loop in matlab

I have an error in the following forloop. I know because the end value of the first for is going to be changed and it is not acceptable for Matlab to change in inside iteration. But would you have any idea how to overcome to it? By the way I used while, but does not help me at all. Data are as follow:
D = [
2.39484592826072e-05 286
4.94140791861196e-05 161
5.07906972800045e-05 163
0.000103133134300751 141
0.000142755898501384 136
0.000143741615840070 152
0.000188072960663613 177
0.000203545320971960 1
0.000269110781516704 296
0.000333161025069404 293
0.000351184122591795 167
0.000393661764751196 299
0.000469154814856272 173
0.000516662289403544 181
0.000537612407901054 156
0.000698464342131732 246
0.000848447859349023 66
0.000875283151707512 75
0.00102377583629824 68
0.00110034589129900 277
0.00110693756077989 129
0.00120680501123819 87
0.00151080017572355 78
0.00159156469379168 248
0.00190852817897233 270
0.00192106167039306 133
0.00224677708557380 258
0.00246430115488258 264
0.00288772180685041 255
0.00299392149856582 81
0.00315341807121748 242
0.00327625233716732 27
0.00362308575885149 124
0.00434568780796603 220
0.00443389247698617 239
0.00470947127244510 60
0.00474015278667278 23
0.00481651908877289 230
0.00487750364266560 53
0.00510342992049100 56
0.00513758569662983 228
0.00515453564704144 121
0.00515656244518627 232
0.00526922882200147 8
0.00547349131456174 50
0.00553337871530176 117
0.00569159206242299 18
0.00620144292620718 13
0.00630382865700000 119
0.00755647842280271 92
0.00983041839684126 40
0.00997057619578698 98
0.0102611966834032 44
0.0103337998140422 100
0.0105132461082006 37
0.0106952804631761 109
0.0107424055503829 208
0.0109630950142485 111
0.0115094667290339 105
0.0119529682389369 107];
ymin= D(:,1);
mean_value = 0.00773867192661190;
criteria = min(ymin);
kk = 1;
diff = 60;
and here is the code that I would have an error for the changing size_D which is expected.
while criteria < mean_value
if isempty(B)
ind_crt = find(min(ymin));
B(kk,:) = D(ind_crt,:);
D(ind_crt,:) = [];
kk = kk + 1;
end
criteria = min(min(D));
size_D = size(D,1);
for ii=1:size_D
if D(ii,1) == criteria
size_B = size(B,1);
for jj = 1:size_B
if abs(D(ii,2) - B(jj,2)) > diff
B(kk,:) = D(ii,:);
D(ii,:)= [];
kk = kk + 1;
end
size_D = size_D -1;
criteria = min(min(D))
end
end
end
end
Update:
Here is the error:
Attempted to access D(59,1); index out of bounds because
size(D)=[58,2].
Error in local_minima (line 50)
if D(ii,1) == criteria
Replace your for loop by a while loop, so that the code in the loop is run only if the condition ii<=size_D is verified:
ii=0;
while ii<=size_D
ii=ii+1;
%loop code
instead of the
for ii=1:size_D
%loop code

How to reshape a matrix horizontally using MATLAB

I have matrix A of the size(4,192). It consists of 12 matrices of the size(4,4) aligned horizontally. I want to get matrix B with the size(12,16). B must get as follows:
suppose
A=[y1,y2,y3,...,y12]
in which yn is a 4*4 matrix. Then,
B=[y1,y4,y7,y10;
y2,y5,y8,y11;
y3,y6,y9,y12]
Is there an efficient/quicker (using no loop) way to do this using MATLAB?
You can try the following code:
ys1 = 2; % size(1) from submatrix (for the following example, use ys1 = 4 for the actual problem)
ys2 = 2; % size(2) from submatrix (for the following example, use ys2 = 4 for the actual problem)
ns1 = 3; % size(1) of final matrix in terms of submatrix (3 rows)
ns2 = 4; % size(2) of final matrix in terms of submatrix (4 columns)
temp = reshape(A,ys1,ys2,ns1,ns2);
B = reshape(permute(temp,[1 3 2 4]),ys1*ns1,ys2*ns2);
Example:
A = [11 12 21 22 31 32 41 42 51 52 61 62 71 72 81 82 91 92 101 102 111 112 121 122;
13 14 23 24 33 34 43 44 53 54 63 64 73 74 83 84 93 94 103 104 113 114 123 124];
B =
11 12 41 42 71 72 101 102
13 14 43 44 73 74 103 104
21 22 51 52 81 82 111 112
23 24 53 54 83 84 113 114
31 32 61 62 91 92 121 122
33 34 63 64 93 94 123 124

Matlab getting point coordinates

How can I get the coordinates at a specific point?
I want to get the X coordinate of the points with Y = 18.1 ; Y = 33; Y = 70
Those points need to lie on the function that I plot.
Sample Code
t = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
y = [0 5 9 19 25 32 46 65 79 90 100 115 123 141 153 159 160 171 181 185 193 200 205 211 215 220 223 222 225 224 228 231 231 228 235 234 231];
plot(t,y) , grid on
Unfortunately, you are trying to find the values of t associated with a y, and your function is not monotonic so we need to actually code up a mock linear interpolation. Note, there may be a better way, but I do not know of it right now. Try the following code where yVals are the values you want an associated t for, and possArray will include all values of t that may satisfy those conditions.
clc; close all; clear all;
t = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
y = [0 5 9 19 25 32 46 65 79 90 100 115 123 141 153 159 160 171 181 185 193 200 205 211 215 220 223 222 225 224 228 231 231 228 235 234 231];
plot(t,y)
grid on
hold on
yVals = [18.1,33,70,222.5,230];
possArray = cell(1,numel(yVals));
iter = 1;
for val = yVals;
poss = [];
possNum = 1;
for i = 1:numel(y)-1
if y(i) <= val && y(i+1) >= val
minDiff = val-y(i);
yDiff = y(i+1)-y(i);
percAlong = minDiff/yDiff;
poss(possNum) = (t(i+1)-t(i))*percAlong+t(i);
possNum = possNum+1;
end
end
possArray{iter} = poss;
iter = iter + 1;
end
colors = hsv(numel(yVals));
legendCell = cell(numel(yVals)+1,1);
legendCell{1} = 'Original Line';
for i = 1:numel(yVals)
plot(possArray{i},yVals(i)*ones(size(possArray{i})),...
'x','MarkerSize',10,'LineWidth',2,'Color',colors(i,:))
legendCell{i+1} = ['Values for Y = ' ,num2str(yVals(i))];
end
legend(legendCell)
hold off
As stated previously, this is linear interpolation, so if you need it to be more complicated that is on you, the concept should however be similar
UPDATE
Updated code above to be a little more clean, and added a plot indicating that multiple possibilities may arise for a single value, and that the code will return all possibilities.