using recursion for solving Euler 18 in q - kdb

I have written this code in q for solving Euler 18 problem,as described in the link below, using recursion.
https://stackoverflow.com/questions/8002252/euler-project-18-approach
Though the code works, it is not efficient and gets stack overflow at pyramids of sizes greater than 3000. How could I make this code much more efficient.I believe the optimum code can be less than 30 characters.
pyr:{[x]
lsize:count x;
y:x;
$[lsize <=1;y[0];
[.ds.lastone:x[lsize - 1];
.ds.lasttwo:x[lsize - 2];
y:{{max (.ds.lasttwo)[x] +/: .ds.lastone[x],.ds.lastone[x+1]}each til count .ds.lasttwo};
$[(count .ds.lasttwo)=1;y:{max (.ds.lasttwo) +/: .ds.lastone[x],.ds.lastone[x+1]}0;y:y[]];
x[lsize - 2]:y;
pyr[-1_x]]]
}

To properly implement this logic in q you need to use adverbs.
First, to quickly find the rolling maximums you can use the prior adverb. For example:
q)input:(75;95 64;17 47 82;18 35 87 10;20 04 82 47 65;19 01 23 75 03 34;88 02 77 73 07 63 67;99 65 04 28 06 16 70 92;41 41 26 56 83 40 80 70 33;41 48 72 33 47 32 37 16 94 29;53 71 44 65 25 43 91 52 97 51 14;70 11 33 28 77 73 17 78 39 68 17 57;91 71 52 38 17 14 91 43 58 50 27 29 48;63 66 04 68 89 53 67 30 73 16 69 87 40 31;04 62 98 27 23 09 70 98 73 93 38 53 60 04 23)
q)last input
4 62 98 27 23 9 70 98 73 93 38 53 60 4 23
q)1_(|) prior last input
62 98 98 27 23 70 98 98 93 93 53 60 60 23
That last line outputs the a vector with the maximum value between each successive pair in the input vector. Once you have this you can add it to the next row and repeat.
q)foo:{y+1_(|) prior x}
q)foo[input 14;input 13]
125 164 102 95 112 123 165 128 166 109 122 147 100 54
Then, to apply this function over the whole use the over adverb:
q)foo over reverse input
,1074
EDIT: The approach above can be generalized further.
q provides a moving max function mmax. With this you can find "the x-item moving maximum of numeric y", which generalizes the use of prior above. For example, you can use this to find the moving maximum of pairs or triplets in the last row of the input:
q)last input
4 62 98 27 23 9 70 98 73 93 38 53 60 4 23
q)2 mmax last input
4 62 98 98 27 23 70 98 98 93 93 53 60 60 23
q)3 mmax last input
4 62 98 98 98 27 70 98 98 98 93 93 60 60 60
mmax can be used to simplify foo above:
q)foo:{y+1_ 2 mmax x}
What's especially nice about this is that it can be used to generalize to variants of this problem with wider triangles. For example, the triangle below has two more values on each row and from any point on a row you can move to the left, middle, or right of the row below it.
5
5 6 7
6 7 3 9 1

Related

How to sum columns of a matrix for a specified number of columns?

I have a matrix A of size 2500 x 500. I want to sum each 10 columns and get the result as a matrix B of size 2500 x 50. That is, the first column of B is the sum of the first 10 columns of A, the second column of B is the sum of second 10 columns of A, and so on.
How can I do that without a for loop? Since I have to do that hundreds of times and it is highly time consuming to do that using for loop.
First, we "block reshape" A, such that we have the desired number of columns. Therefore, we shamelessly steal the code from the great Divakar, and put in some minimal effort to generalize it. Then, we just need to sum along the second axis, and reshape to the original form.
Here's an example with five columns to be summed:
% Sample input data
A = reshape(1:100, 10, 10).'
[r, c] = size(A);
% Number of columns to be summed
n_cols = 5;
% Block reshape to n_cols, see https://stackoverflow.com/a/40508999/11089932
B = reshape(permute(reshape(A, r, n_cols, []), [1, 3, 2]), [], n_cols);
% Sum along second axis
B = sum(B, 2);
% Reshape to original form
B = reshape(B, r, c / n_cols)
That's the output:
A =
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
B =
15 40
65 90
115 140
165 190
215 240
265 290
315 340
365 390
415 440
465 490
Hope that helps!
This can be done with splitapply. An advantage of this approach is that it works even if the group size does not divide the number of columns (the last group is smaller):
A = reshape(1:120, 12, 10).'; % example 10×12 data (borrowed from HansHirse)
n_cols = 5; % number of columns to sum over
result = splitapply(#(x)sum(x,2), A, ceil((1:size(A,2))/n_cols));
In this example,
A =
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
result =
15 40 23
75 100 47
135 160 71
195 220 95
255 280 119
315 340 143
375 400 167
435 460 191
495 520 215
555 580 239

Postgres Large array import

I just received a large data array of random numbers. 20 numbers per line, 600,000 lines in a CSV file. The numbers are separated by a space instead of a comma thus postgresql reads it as one long string per line and I cannot insert the proper data attribute for the data.
Each set of numbers will have a unique id. Each number is 2 digits long. I want the ability to count the amount of times a certain number was entered. Get the frequency of each number between certain ID's.
My question:
What data type should I use to insert the data so it is recognized as integers instead of text?
How do I replace the space with a comma?
Do I need to replace the space with a comma?
Currently running Postgres 9.6, PgAdmin 4.
Bonus if answer is provided in PgAdmin as well.
Also here is a sample
Excel
Numbers
06 18 20 21 24 32 36 40 44 47 50 52 55 57 60 61 62 68 72 79
03 05 12 13 14 16 17 18 24 28 33 34 35 39 44 55 62 63 64 67
09 10 12 13 15 25 30 31 36 42 43 44 46 48 51 57 65 69 75 79
08 12 15 20 27 33 34 37 41 43 44 45 54 55 60 61 66 70 72 76
CSV FILE
Numbers06 18 20 21 24 32 36 40 44 47 50 52 55 57 60 61 62 68 72 79
03 05 12 13 14 16 17 18 24 28 33 34 35 39 44 55 62 63 64 67
09 10 12 13 15 25 30 31 36 42 43 44 46 48 51 57 65 69 75 79
08 12 15 20 27 33 34 37 41 43 44 45 54 55 60 61 66 70 72 76
or the file with the id numbers
CSV
ID, Numbers
1253842,06 18 20 21 24 32 36 40 44 47 50 52 55 57 60 61 62 68 72 79
1253843,03 05 12 13 14 16 17 18 24 28 33 34 35 39 44 55 62 63 64 67
1253844,09 10 12 13 15 25 30 31 36 42 43 44 46 48 51 57 65 69 75 79
1253845,08 12 15 20 27 33 34 37 41 43 44 45 54 55 60 61 66 70 72 76
1253846,04 06 07 09 11 12 13 14 18 20 21 26 30 36 37 41 43 48 74 79
1253847,01 11 14 15 35 37 38 43 46 48 49 51 53 57 64 65 66 70 76 77
1253848,01 03 14 17 20 22 24 25 38 42 46 54 56 57 60 61 66 72 78 80
Here's the error message
>
ERROR: malformed array literal: "06 18 20 21 24 32 36 40 44 47 50 52 55 57 60 61 62 68 72 79"
DETAIL: Array value must start with "{" or dimension information.
CONTEXT: COPY Quick numbers, line 2, column numbers : "06 18 20 21 24 32 36 40 >44 47 50 52 55 57 60 61 62 68 72 79"

Matlab: 3D Surface Plot of values from own vectors

I have 3 vectors with values for coordinates (X,Y,Z) and I want to plot them as a surface. I have tried all sorts of solutions from here and other forums and cannot for the life of me get it to look like anything that makes sense. I have a picture that can describe the situation better but I can't post it as I don't have enough reputation. Please help. Thank you.
EDIT: This is the link to the picture :
In the picture, the origin of each vector arrow represents the point in a 3D coordinate, with the length of the vector arrow giving the magnitude of the required torque to move at that point in 3D space.
Right now, the data is separated in line vectors with each point's coordinate: so that's one vector for the X, one for Y. Z is one line inside a 3 line matrix as the whole matrix describes the required torque in a 3 coordinate axes.
I've tried using meshgridon the X and Y vectors and then attributed the Z value using griddataand then surfbut I'm not getting something that looks like the original:
The plot is linear but I'm pretty sure the data is not... at least not that linear.
It seems like you want want to plot the single points decribed by the vectors, here you have an old answer of mine (as a bonus you will have nice colorful plots 'cause that's what the original question asked):
Assuming Data=[Vec1,Vec2,Vec3,...] and VecN=[Xn,Yn,Zn]'
"
If you want to plot points, you can define an RGB color and plot single points with hold on like this:
hold on
for i=1:length(Data(:,1))
plot3(Data(i,1),Data(i,2),Data(i,3),'Color',[(i/100*255)/255 0/255 (255-(i/100*255))/255],'LineWidth',2)
end
shg
"
Managed to solve the problem. As mentioned, the data was not uniform and because of that, surfwas jumping from one end of the plot to the other, creating a total mess. Solved it by organising the values linearly using linspaceand then using those values to create the meshgrid and then assign the Z values using griddata with a cubic interpolation.. Managed to produce proper looking surface plots with the data on hand.
Plot each data point separately within a loop:
figure; hold on; grid on;
for i = 1:length(x)
stem3(x(i),y(i),z(i));
end
Don't forget to add hold on. Use "Rotate 3D" button from the toolbar in the figure window if the plot was shown in 2D at first.
You can use griddata parameter to natural, cubic or v4. This will do interpolation and make your graph with non-uniform data smooth
Below is a sample MATLAb code
x=[32 20 67 1 98 34 57 65 24 82 47 55 8 51 13 14 18 30 37 39 10 33 21 26 38 81 83 60 95 22 17 5 72 46 99 52 12 25 96 29 70 85 43 69 19 78 97 31 89 53 2 91 48 71 61 15 36 84 94 50 11 80 6 7 49 74 9 88 40 79 27 68 73 64 63 59 86 23 35 58 45 28 100 42 93 87 16 90 41 66 54 92 77 4 62 76 75 56 3 44];
y=[96 75 24 9 83 49 27 77 3 23 17 31 40 13 7 52 51 21 98 47 64 79 78 91 44 16 15 100 84 99 63 68 70 30 54 76 97 73 33 5 88 8 71 66 62 25 60 42 72 45 18 11 28 59 89 65 10 55 69 81 12 26 20 95 87 41 74 50 93 22 43 90 14 34 82 35 56 38 80 32 1 57 6 36 37 61 29 58 2 48 4 46 67 53 92 86 94 19 39 85];
z=[55 31 11 45 83 36 86 49 15 57 42 46 8 94 88 47 54 81 98 41 32 35 56 85 9 89 37 60 23 62 67 100 78 76 73 80 10 20 68 34 77 93 1 63 53 12 22 99 91 40 84 24 33 3 43 19 92 97 6 82 64 25 26 79 95 4 44 58 5 21 70 29 65 87 96 90 51 14 18 2 72 28 71 39 52 7 27 59 50 61 48 30 66 69 17 13 74 16 75 38];
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)

efficient and accurate matrix search method in matlab

I'm brushing off my MATLAB skills, which haven't been used it a very long time. And to do so I've been doing the puzzles over at project eueler. Well, I'm kind of stumped on this one as everything seems to run fine when I break into parts but my greatest number is apparently not correct.
Anyway here's my code
%This script will take a grid and find the greatest product of 4 numbers
%up, down, left right, and diagonal
%create the grid
grid = [08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08;
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00;
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65;
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91;
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80;
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50;
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70;
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21;
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72;
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95;
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92;
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57;
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58;
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40;
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66;
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69;
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36;
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16;
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54;
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48;];
%grid(row, column)
%find how many rows and columns there are
[rowNum, columnNum] = size(grid);
%Current greatest product of four consecutive
greatest = 0;
%test left right
%iterate through all rows
for i = 1:rowNum
%iterate through all columns except the last tthree
for x = 1:columnNum-3
%test the consecutive numbers starting with the current colum
%iteration
current = 1;
for test = x:x+3
current = current * grid(i, test);
if greatest < current
greatest = current;
end
end
end
end
%iterate through all columns
for i = 1:columnNum
%iterate throw all rows except the last three
for x = 1:rowNum-3
%test the consecutive numbers starting with the current colum
%iteration
current = 1;
for test = x:x+3
current = current * grid(test, i);
if greatest < current
greatest = current;
end
end
end
end
for i = 1:columnNum-3
%iterate throw all column except the last two
for x = 1:rowNum-3
%test the consecutive numbers starting with the current colum
%iteration
current = 1;
%count adds the number of iterations to the row number in grid
%forcing it to move in a diagonal line
count = 0;
%test consecutively from x to three to the right
for test = x:x+3
%make new current product
current = current * grid(i+count, test);
%add to count to shift down one row next iteration
count = count + 1;
%check for greatest
if greatest < current
greatest = current;
end
end
end
end
disp(greatest)
I think the problem lies in the nested for loops searching the diagonal, but I'm not sure.
I've broken it up into pieces (test each for loop on its own and it it output each element as it searched through them). I'm pretty sure the horizontal and vertical search method works correctly.
Just wanted a fresh set of eyes to look at this, Thanks!
Also I'm sure there is a more efficient way to do this so if anyone has some ideas for a better algorithm I'd love to hear it!

How can I retrieve the coordinate from excel and plot the points according to the map? - MATLAB

I'm currently working on map routing system for pedestrian pathway and not sure does anyone did this before. But I have collected various way-points coordinates of the path using google earth.
Below data are the coordinates points for the pathway which is save it on Excel and import to Matlab
1 1.37723400000000 103.839645000000
2 1.37722000000000 103.839741000000
3 1.37723300000000 103.839843000000
4 1.37723600000000 103.839968000000
5 1.37724100000000 103.840125000000
6 1.37723900000000 103.840245000000
7 1.37724900000000 103.840435000000
8 1.37725700000000 103.840674000000
9 1.37724800000000 103.840896000000
10 1.37726500000000 103.841071000000
11 1.37728300000000 103.841190000000
12 1.37729500000000 103.841393000000
13 1.37734600000000 103.841591000000
14 1.37736500000000 103.841918000000
15 1.37739800000000 103.842093000000
16 1.37742900000000 103.842400000000
17 1.37744900000000 103.842629000000
18 1.37748100000000 103.842895000000
19 1.37750200000000 103.843164000000
20 1.37752800000000 103.843428000000
21 1.37756500000000 103.843689000000
22 1.37757000000000 103.843997000000
23 1.37758500000000 103.844248000000
24 1.37749200000000 103.844499000000
25 1.37765800000000 103.844515000000
26 1.37806100000000 103.844467000000
27 1.37853700000000 103.844386000000
28 1.37888000000000 103.844349000000
29 1.37924600000000 103.844303000000
30 1.37957500000000 103.844266000000
31 1.37966200000000 103.844190000000
32 1.37965300000000 103.844034000000
33 1.37963800000000 103.843813000000
34 1.37961600000000 103.843550000000
35 1.37959500000000 103.843313000000
36 1.37957600000000 103.843097000000
37 1.37957300000000 103.843007000000
38 1.37956800000000 103.842852000000
39 1.37953900000000 103.842602000000
40 1.37949900000000 103.842315000000
41 1.37948700000000 103.842056000000
42 1.37953800000000 103.841882000000
43 1.37967800000000 103.841755000000
44 1.37984000000000 103.841674000000
45 1.38007300000000 103.841546000000
46 1.38027900000000 103.841430000000
47 1.38044400000000 103.841354000000
48 1.38064800000000 103.841248000000
49 1.38092400000000 103.841100000000
50 1.38125600000000 103.840942000000
51 1.38178300000000 103.840682000000
52 1.38202300000000 103.840565000000
53 1.38236700000000 103.840396000000
54 1.38271600000000 103.840206000000
55 1.38302800000000 103.840017000000
56 1.38314600000000 103.840222000000
57 1.38299000000000 103.840230000000
58 1.38286700000000 103.840288000000
59 1.38265300000000 103.840398000000
60 1.38242100000000 103.840522000000
61 1.38219900000000 103.840638000000
62 1.38195100000000 103.840766000000
63 1.38162100000000 103.840934000000
64 1.38130200000000 103.841093000000
65 1.38106700000000 103.841202000000
66 1.38088500000000 103.841286000000
67 1.38069300000000 103.841372000000
68 1.38049000000000 103.841494000000
69 1.38018700000000 103.841659000000
70 1.37988900000000 103.841822000000
71 1.37966700000000 103.841963000000
72 1.37965000000000 103.842037000000
73 1.37965100000000 103.842220000000
74 1.37966300000000 103.842363000000
75 1.37968900000000 103.842641000000
76 1.37973000000000 103.843049000000
77 1.37975300000000 103.843305000000
78 1.37978200000000 103.843690000000
79 1.37982300000000 103.844135000000
80 1.37989800000000 103.844222000000
81 1.37996400000000 103.844230000000
82 1.38029900000000 103.844183000000
83 1.38080600000000 103.844091000000
84 1.38119600000000 103.843985000000
85 1.38170500000000 103.843838000000
86 1.38194900000000 103.843765000000
87 1.38220500000000 103.843683000000
88 1.38250000000000 103.843564000000
89 1.38296800000000 103.843388000000
90 1.38367400000000 103.843107000000
91 1.38379700000000 103.842994000000
92 1.38384900000000 103.842791000000
93 1.38376700000000 103.842173000000
94 1.38372500000000 103.841758000000
95 1.38358400000000 103.841212000000
96 1.38350400000000 103.840867000000
97 1.38324600000000 103.840388000000
98 1.38288200000000 103.839785000000
99 1.38265700000000 103.839436000000
100 1.38238700000000 103.838973000000
101 1.38224900000000 103.838785000000
102 1.38215800000000 103.838830000000
103 1.38197900000000 103.838933000000
104 1.38162900000000 103.839097000000
105 1.38115400000000 103.839338000000
106 1.38080900000000 103.839609000000
107 1.38036600000000 103.839756000000
108 1.37995000000000 103.839782000000
109 1.37957400000000 103.839826000000
110 1.37904400000000 103.839883000000
111 1.37844300000000 103.839807000000
112 1.37803700000000 103.839743000000
113 1.37765800000000 103.839690000000
114 1.37735600000000 103.839665000000
115 1.38310100000000 103.840134000000
116 1.38108800000000 103.841146000000
117 1.37961700000000 103.842951000000
118 1.37978200000000 103.844233000000
My Code
M = xlsread('YCKMap.xlsx');
figure(),plot(M(:,2), M(:,3));
RESULT
http://i.stack.imgur.com/0dncY.jpg
What I want:
Is it possible to join these points together
(55 115 56) (49 116 65) (37 117 76) (30 118 80)
As shown in the image above?
I have figure out the answer for my question and would like to benefit the community for what I have come out with. According #Dan code plot(M(:,2), M(:,3); can plot the points out but the orientation of the graph is way off. Therefore I use waypoints function to make it better.
Anyway, organize your excel data by leaving a cell gap when you want to insert a new line.
I have paste the excel data for you to get a clearer picture.
Coding
M = xlsread('excel.xlsx');
waypoints =[M(:,2), M(:,3)];
[lttrk,lntrk] = track('rh',waypoints,'degrees');
figure(),geoshow(lttrk,lntrk,'DisplayType','line', 'color','r');
Excel Data
1 1.377234 103.839645
2 1.37722 103.839741
3 1.377233 103.839843
4 1.377236 103.839968
5 1.377241 103.840125
6 1.377239 103.840245
7 1.377249 103.840435
8 1.377257 103.840674
9 1.377248 103.840896
10 1.377265 103.841071
11 1.377283 103.84119
12 1.377295 103.841393
13 1.377346 103.841591
14 1.377365 103.841918
15 1.377398 103.842093
16 1.377429 103.8424
17 1.377449 103.842629
18 1.377481 103.842895
19 1.377502 103.843164
20 1.377528 103.843428
21 1.377565 103.843689
22 1.37757 103.843997
23 1.377585 103.844248
24 1.377492 103.844499
25 1.377658 103.844515
26 1.378061 103.844467
27 1.378537 103.844386
28 1.37888 103.844349
29 1.379246 103.844303
30 1.379575 103.844266
31 1.379662 103.84419
32 1.379653 103.844034
33 1.379638 103.843813
34 1.379616 103.84355
35 1.379595 103.843313
36 1.379576 103.843097
37 1.379573 103.843007
38 1.379568 103.842852
39 1.379539 103.842602
40 1.379499 103.842315
41 1.379487 103.842056
42 1.379538 103.841882
43 1.379678 103.841755
44 1.37984 103.841674
45 1.380073 103.841546
46 1.380279 103.84143
47 1.380444 103.841354
48 1.380648 103.841248
49 1.380924 103.8411
50 1.381256 103.840942
51 1.381783 103.840682
52 1.382023 103.840565
53 1.382367 103.840396
54 1.382716 103.840206
55 1.383028 103.840017
56 1.383146 103.840222
57 1.38299 103.84023
58 1.382867 103.840288
59 1.382653 103.840398
60 1.382421 103.840522
61 1.382199 103.840638
62 1.381951 103.840766
63 1.381621 103.840934
64 1.381302 103.841093
65 1.381067 103.841202
66 1.380885 103.841286
67 1.380693 103.841372
68 1.38049 103.841494
69 1.380187 103.841659
70 1.379889 103.841822
71 1.379667 103.841963
72 1.37965 103.842037
73 1.379651 103.84222
74 1.379663 103.842363
75 1.379689 103.842641
76 1.37973 103.843049
77 1.379753 103.843305
78 1.379782 103.84369
79 1.379823 103.844135
80 1.379898 103.844222
81 1.379964 103.84423
82 1.380299 103.844183
83 1.380806 103.844091
84 1.381196 103.843985
85 1.381705 103.843838
86 1.381949 103.843765
87 1.382205 103.843683
88 1.3825 103.843564
89 1.382968 103.843388
90 1.383674 103.843107
91 1.383797 103.842994
92 1.383849 103.842791
93 1.383767 103.842173
94 1.383725 103.841758
95 1.383584 103.841212
96 1.383504 103.840867
97 1.383246 103.840388
98 1.382882 103.839785
99 1.382657 103.839436
100 1.382387 103.838973
101 1.382249 103.838785
102 1.382158 103.83883
103 1.381979 103.838933
104 1.381629 103.839097
105 1.381154 103.839338
106 1.380809 103.839609
107 1.380366 103.839756
108 1.37995 103.839782
109 1.379574 103.839826
110 1.379044 103.839883
111 1.378443 103.839807
112 1.378037 103.839743
113 1.377658 103.83969
114 1.377356 103.839665
1 1.377234 103.839645
49 1.380924 103.8411
116 1.381088 103.841146
65 1.381067 103.841202
37 1.379573 103.843007
117 1.379617 103.842951
76 1.37973 103.843049
30 1.379575 103.844266
118 1.379782 103.844233
80 1.379898 103.844222
If you want to plot points in Matlab in a different order from that which they appear in your matrix, append a column vector at the beginning of the matrix that is a vector of the order the point should appear in. Then use the sortrows function of matlab to rearrange the matrix. It will now be in the correct order for plotting. For example:
M = [0 4 2; 1 17 5]';
plot(M);
Now suppose you knew that the correct plot order was (0,1) then (2,5) then (4,17). Compared to the input matrix, M, the correct plot order can be specified by order = [1 3 2]'
So to plot in the correct order:
M = [order'; M'];
M = sortrows(M);
plot(M(:,2), M(:,3);
Which means as long as you know the correct plot order, you can now use this method to get the correct plot.
So in your example you want those points to be plotted in this order (55 115 56) (49 116 65) (37 117 76) (30 118 80). Lets assume you want them to start at 55 as this is the first point in your new order, currently M looks like this: M = [...;30 118 80;...;37 117 76;...;49 116 65;...;55 115 56;...] but you want it to look like this: M = [...; 55 115 56; 56 116 65; 57 117 76; 58 118 80;...]. The problem is that you can't just change the values like this M(M(:,1) == 30,1) = 58 because then you will land up with 2 points indexed at 58. So first you need to add 1 too all the indices that are 58 or higher, so M(M(:,1) >= 58) = M(M(:,1) >= 58) + 1 and then finally M(M(:,1) == 30,1) = 58. So to generalise the changing of a point:
M(M(:,1) >= newIndex) = M(M(:,1) >= newIndex) + 1;
M(M(:,1) == oldIndex,1) = newIndex;
If there is some logic that define how you change the points, then you can write this into a loop. If not and it is just those 4 points, then just run this manually in the command line.