Saving (in a matrix) the elapsed time and number of iterations for a large number of cases - matlab

I have a program that outputs the number of iterations and a test value, given inputs A1,A2,A3,A4.
I want to run through 5 values each of A1, A2, A3, A4, thus making 625 runs. In the process, I want to save the time elapsed for each run, the number of iterations, and test value in 3 separate matrices.
I have tried using 4 nested for loops, and made progress, but need some help on indexing the elements of the matrices. The iterator variables in the for loops don't match the indexing variables...
The code for the 4 nested loops is below:
m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(5,length(m)*length(m)*length(m));
for A1 = m
for A2 = m
for A3 = m
for A4 = n
tic
SmallMAX(A1,A2,A3,A4)
toc;
for i=1:numel(eltime)
for j = 1:length(n)
eltime(j,i) = toc;
end
end
end
end
end
end
The code for the main program is excerpted below:
function [k,test] = SmallMAX(A1,A2,A3,A4)
...
end
Thanks for any help.

In your case, the easiest way is to use A1, A2, A3 and A4 as counters instead of the actual values. This way you them to index the entries of eltime. We can then easily calculate the index in the second dimension with sub2ind and use A4 to index the first dimension of eltime. We need to adjust the arguments in SmallMAX as well.
Here is the code of the proposed method:
m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(length(n),length(m)*length(m)*length(m));
res_k = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));
res_test = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));
for A1 = 1:length(m)
for A2 = 1:length(m)
for A3 = 1:length(m)
for A4 = 1:length(n)
ind = sub2ind([length(m),length(m),length(m)],A3,A2,A1);
tic
[k,test] = SmallMAX(m(A1),m(A2),m(A3),n(A4));
eltime(A4,ind) = toc;
res_k(A4,ind) = k;
res_test(A4,ind) = test;
end
end
end
end
This is the order of the addressed entries of eltime:
eltime_order =
Columns 1 through 18
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86
2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 82 87
3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88
4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90
Columns 19 through 36
91 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176
92 97 102 107 112 117 122 127 132 137 142 147 152 157 162 167 172 177
93 98 103 108 113 118 123 128 133 138 143 148 153 158 163 168 173 178
94 99 104 109 114 119 124 129 134 139 144 149 154 159 164 169 174 179
95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180
Columns 37 through 54
181 186 191 196 201 206 211 216 221 226 231 236 241 246 251 256 261 266
182 187 192 197 202 207 212 217 222 227 232 237 242 247 252 257 262 267
183 188 193 198 203 208 213 218 223 228 233 238 243 248 253 258 263 268
184 189 194 199 204 209 214 219 224 229 234 239 244 249 254 259 264 269
185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270
Columns 55 through 72
271 276 281 286 291 296 301 306 311 316 321 326 331 336 341 346 351 356
272 277 282 287 292 297 302 307 312 317 322 327 332 337 342 347 352 357
273 278 283 288 293 298 303 308 313 318 323 328 333 338 343 348 353 358
274 279 284 289 294 299 304 309 314 319 324 329 334 339 344 349 354 359
275 280 285 290 295 300 305 310 315 320 325 330 335 340 345 350 355 360
Columns 73 through 90
361 366 371 376 381 386 391 396 401 406 411 416 421 426 431 436 441 446
362 367 372 377 382 387 392 397 402 407 412 417 422 427 432 437 442 447
363 368 373 378 383 388 393 398 403 408 413 418 423 428 433 438 443 448
364 369 374 379 384 389 394 399 404 409 414 419 424 429 434 439 444 449
365 370 375 380 385 390 395 400 405 410 415 420 425 430 435 440 445 450
Columns 91 through 108
451 456 461 466 471 476 481 486 491 496 501 506 511 516 521 526 531 536
452 457 462 467 472 477 482 487 492 497 502 507 512 517 522 527 532 537
453 458 463 468 473 478 483 488 493 498 503 508 513 518 523 528 533 538
454 459 464 469 474 479 484 489 494 499 504 509 514 519 524 529 534 539
455 460 465 470 475 480 485 490 495 500 505 510 515 520 525 530 535 540
Columns 109 through 125
541 546 551 556 561 566 571 576 581 586 591 596 601 606 611 616 621
542 547 552 557 562 567 572 577 582 587 592 597 602 607 612 617 622
543 548 553 558 563 568 573 578 583 588 593 598 603 608 613 618 623
544 549 554 559 564 569 574 579 584 589 594 599 604 609 614 619 624
545 550 555 560 565 570 575 580 585 590 595 600 605 610 615 620 625

Related

In KDB, how do I sum the previous 3 numbers in a list?

Say I have a list of numbers:
j: (til 40)*9
0 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 189 198 207 216 225 234 243 252 261 270 279 288 297 306 315 324 333 342 351
What's the most elegant way to get the sum of the previous 3 (or n) numbers in the list? (Ideally considering large RAM constrained lists).
Does this work?
q)3 msum j
0 9 27 54 81 108 135 162 189 216 243 270 297 324 351 378 405 432 459 486 513 ..

MATLAB Polygon Self Intersection - Delete Zero Volume Parts

I have a MATLAB polygon (below) which self intersects to make the zero volume part (which I will call a sliver) in the image (also below).
I am having a lot of trouble finding and removing the zero volume sliver.
Hope anyone can help. Thanks.
IMAGE:
enter image description here
Polygon:
newBoundary =
1 1
1 216
8 221
25 239
46 255
60 269
70 282
81 296
92 313
113 323
127 326
145 336
163 349
170 368
174 374
192 388
209 400
229 416
236 433
252 450
266 470
268 488
274 504
284 523
274 518
249 513
246 532
246 538
224 527
216 541
206 554
205 573
192 589
185 590
169 609
148 620
154 641
170 646
172 660
165 683
154 673
140 654
130 648
116 656
105 660
92 679
82 700
69 719
90 730
77 751
87 770
66 763
48 774
37 791
28 804
18 804
9 817
1 834
1 838
1 1049
131 1049
141 1044
164 1029
181 1014
193 1004
208 995
221 979
239 979
239 962
253 951
270 940
290 926
299 908
317 899
332 879
353 869
364 857
379 841
376 856
386 870
383 889
380 913
382 933
383 958
378 975
379 993
378 1016
374 1029
367 1040
366 1049
616 1049
615 1049
610 1037
615 1016
612 993
618 978
612 953
609 936
606 913
608 894
609 874
606 854
622 839
633 853
651 869
670 883
690 900
706 916
721 934
740 951
747 971
765 978
785 992
795 1007
816 1015
831 1028
848 1049
1049 1049
1049 781
1027 784
1003 780
997 798
974 812
973 796
964 780
947 765
930 773
922 760
926 736
917 722
898 713
879 719
883 704
886 679
873 663
853 670
847 649
841 627
839 607
835 621
814 631
794 620
778 604
761 588
744 601
729 592
716 577
705 556
698 537
689 517
698 502
711 486
726 486
730 463
743 446
760 439
764 420
784 404
802 388
816 373
837 366
846 344
866 337
881 324
901 314
912 299
926 283
946 272
951 253
970 242
977 225
998 223
1006 207
1025 192
1049 186
1049 1
789 1
792 6
778 20
774 37
762 49
753 70
736 79
715 90
695 100
672 91
668 109
663 131
668 153
651 156
657 168
648 170
633 153
612 150
613 127
610 114
613 97
615 74
613 54
612 36
613 21
620 1
380 1
379 9
377 34
371 56
379 78
387 98
379 117
376 136
384 151
372 156
350 164
332 174
318 168
327 150
309 139
299 129
281 127
290 108
275 107
261 90
236 76
235 60
219 52
206 35
187 22
168 4
156 1
1 1
Zero volume slivers may occur when there are three or more consecutive points aligned. In this particular case those were:
366 1049
616 1049
615 1049
since they fall on the same horizontal line.
If you want to automate this, you should check every group of three consecutive points to see if they fall on the same line. If they do, you need to discard the one in the middle, according to appearance order.
In this particular case, the one in the middle is:
616 1049

Matlab Spearman Correlation PVAL = 0?

I am conducting Spearman's Correlation with two data sets with 300 objects. These are my variables and commands:
a = [1:300]
b = [1 2 5 11 9 7 24 10 31 23 3 40 6 17 14 20 16 12 33 46 70 37 87 43 98 26 59 58 77 100 35 42 78 80 243 36 33327 4 83 160 163 198 86 94 406 111 28 29 55 113 239 295 110 196 177 32679 229 342 305 300 254 96 210 514 167 172 232 190 117 32081 25 158 19333 241 82 149 159 66 178 24487 68 30 1016 725 266 391 638 348 320 681 242 319 228 381 408 442 202 369 471 821 191 426 8 270 211 2266 619 576 441 680 3431 1167 723 74 318 556 640 395 1059 579 614 212 325 437 323 687 373 599 26637 985 54 84 802 724 154 417 240 1120 818 2309 462 109 104 509 494 427 57 2475 549 396 419 123 580 79 225 1132 351 76 16859 596 862 315 470 992 257 120 409 751 832 285 1534 714 1665 1376 2129 678 416 721 209 31971 183 356 1346 1015 1003 188 1076 1634 608 1056 338 308 145 418 625 1313 121 2484 996 783 329 1185 697 157 1100 175 622 235 456 277 166 2700 1439 461 653 433 540 1191 234 774 1894 1004 741 1062 948 48 99 405 797 237 1104 2286 22620 1429 30672 1808 169 458 22 1115 10660 872 474 1063 88 1727 1017 1107 1398 1519 703 1092 1027 272 263 1152 1770 1099 507 385 2118 19356 1778 2458 410 2110 7522 17166 4065 15136 13294 10876 17174 2434 9898 5663 13594 10506 11552 15635 9322 3223 8949 12388 13216 13851 13852 6696 12177 4700 17199 2067 11110 15486 5664 6593 4701 527 8616 268]
[RHO,PVAL] = corr(b',a','Type', 'Spearman')
RHO =
0.6954
PVAL =
0
Out of the 5 comparisons I made with other data sets of 300 objects, only 1 returned significant P-values. Is there an explanation for this?
I tried a different data set and got a value that was not significant (PVAL > 0.05). I also displayed the answer in a long (15 digits) and exponential form and got 0.00000000000000e+000 using:
format longEng
I also checked with another statistics program that reported the p-value as < 0.0001. This means that the p-value is just really, really small.

How to delete all numbers in a vector which are smaller than any previous number?

I have data set like:
1 14.8759
2 14.083
3 0.735268
4 18.2378
5 17.3748
6 4.07867
7 18.2032
8 15.6929
9 4.03338
10 19.0308
11 17.4139
12 17.4139
13 19.8453
14 4.91288
15 20.6746
16 16.578
17 14.8548
18 23.9831
19 19.0691
20 19.0777
21 3.24368
22 25.6457
23 -5.95598
24 32.3198
25 8.20419
26 22.3266
27 17.4016
28 9.0672
29 24.8722
30 24.8262
31 19.8966
32 34.7338
33 29.8088
34 33.1393
35 28.1402
36 35.6231
37 26.4872
38 3.2392
39 5.73463
40 26.4754
41 33.9667
42 27.3048
43 34.75
44 37.2759
45 15.6929
46 28.9686
47 44.6922
48 37.2799
49 25.699
50 45.4923
51 32.2579
52 25.699
53 29.7885
54 50.4719
55 20.6746
56 30.6061
57 38.0448
58 11.5342
59 52.9365
60 44.7128
61 38.0448
62 44.6621
63 13.1939
64 28.9542
65 46.3637
66 13.1939
67 10.7318
68 31.4318
69 29.7885
70 22.3399
71 29.7885
72 26.4754
73 55.4135
74 48.8326
75 42.2395
76 19.0174
77 7.4035
78 13.1939
79 33.9055
80 14.8935
81 27.3048
82 6.56548
83 64.4474
84 48.7848
85 59.5214
86 31.4915
87 59.5214
88 19.8966
89 57.0318
90 21.5631
91 20.7273
92 66.0889
93 58.6749
94 20.6803
95 52.1244
96 16.5242
97 51.3028
98 10.7037
99 12.3958
100 26.5265
101 30.6061
102 74.2826
103 50.4806
104 12.3958
105 17.354
106 40.5832
107 19.8514
108 63.6089
109 27.3559
110 9.06318
111 11.564
112 39.7561
113 29.8368
114 17.3615
115 19.0241
116 69.3539
117 35.6231
118 38.8777
119 34.7394
120 60.3455
121 25.6969
122 54.5637
123 25.6969
124 79.2023
125 31.4876
126 28.184
127 13.2268
128 34.7394
129 12.3602
130 29.0096
131 47.9604
132 82.4815
133 77.5533
134 14.8935
135 33.9055
136 16.5172
137 41.4113
138 34.7956
139 64.4558
140 29.8368
141 19.0108
142 26.5265
143 36.4452
144 50.4761
145 4.87781
146 83.3041
147 61.9694
148 26.5265
149 1.5427
150 71.8344
151 24.8158
152 94.7328
153 19.8915
154 36.4452
155 32.2504
156 26.5265
157 89.0202
158 29.8347
159 93.9223
160 87.3855
161 4.89738
162 88.1694
163 24.0448
164 51.2987
165 65.2679
166 89.8386
167 33.9055
168 67.7414
169 88.9942
170 19.0174
171 92.2651
172 49.6527
173 18.1971
174 19.0108
175 33.9667
176 92.2611
177 32.2789
178 92.2577
179 4.89738
180 102.898
181 34.7956
182 95.5292
183 28.9542
184 91.451
185 25.6457
186 74.2944
187 25.6516
188 47.1323
189 34.7338
190 94.7081
191 97.9775
192 105.334
193 89.812
194 93.8991
195 88.1756
196 10.7318
197 49.611
198 97.1618
199 2.40369
200 44.7128
201 35.6263
202 42.1795
203 53.7678
204 70.2067
205 28.9542
206 19.0241
207 111.849
208 19.8915
209 95.5218
210 38.8723
211 101.238
212 19.8393
213 92.2651
214 102.053
215 24.8221
216 116.713
217 88.9912
218 88.1756
219 115.102
220 58.6995
221 19.8393
222 27.3171
223 23.1511
224 53.7678
225 99.6138
226 120.79
227 32.2579
228 90.6265
229 38.0448
230 48.8284
231 111.054
232 112.608
233 66.9162
234 100.431
235 63.6317
236 19.8334
237 35.6263
238 17.3615
239 2.39774
240 29.7885
241 71.0225
242 66.9162
243 25.6457
244 128.908
245 12.3602
246 93.8991
247 123.218
248 24.8221
249 33.1393
250 110.194
251 31.4547
252 12.3958
253 92.2611
254 10.7037
255 90.6302
256 96.3458
257 102.053
258 37.2167
259 93.0788
260 19.0108
261 102.063
262 16.5617
263 49.611
264 135.388
265 117.522
266 92.2879
267 118.378
268 116.706
269 24.0448
270 128.941
271 132.182
272 137.009
273 48.7848
274 32.2789
275 137.826
276 137.009
277 117.522
278 54.5904
279 16.5172
280 141.064
281 63.6317
282 27.3559
283 108.587
284 38.8723
285 140.247
286 106.13
287 135.426
288 67.7371
289 19.8915
290 112.652
291 27.3227
292 117.522
and want to ignore/delete any Y value which is smaller than its previous value (and delete its corresponding X too) and put the new data set into a new file so that all resulted Y values would be in increasing order.
Thanks.
Assuming:
data = [1 14.8759
2 14.083
3 0.735268
... ... ];
You could do that:
keep = false(size(data, 1), 1);
largest = -Inf;
for i = 1:size(data, 1)
if data(i,2) > largest
largest = data(i,2);
keep(i) = true;
end
end
newdata = data(keep,:)
Result:
newdata =
1.0000 14.8759
4.0000 18.2378
10.0000 19.0308
13.0000 19.8453
15.0000 20.6746
18.0000 23.9831
22.0000 25.6457
24.0000 32.3198
32.0000 34.7338
36.0000 35.6231
44.0000 37.2759
47.0000 44.6922
50.0000 45.4923
54.0000 50.4719
59.0000 52.9365
73.0000 55.4135
83.0000 64.4474
92.0000 66.0889
102.0000 74.2826
124.0000 79.2023
132.0000 82.4815
146.0000 83.3041
152.0000 94.7328
180.0000 102.8980
192.0000 105.3340
207.0000 111.8490
216.0000 116.7130
226.0000 120.7900
244.0000 128.9080
264.0000 135.3880
272.0000 137.0090
275.0000 137.8260
280.0000 141.0640
If you've lot of data then it will be better to use vectorization. Removing for loops will make it faster.
Let's say 'A' is your second column (data).
A = 5 4 8 8 2 5 5 7 8 8;
Since your first column is just index we can leave it for now (Even if it's not you can copy second column to 'A' and proceed).
B = A - [-inf A(1:end-1)];
Aout = [find(B>=0);A(B>=0)];
If your first column is not just index copy it to say 'C' and change the last line to the following.
Aout = [C(B>=0);A(B>=0)];
Use bsxfun to compare each element with all the others, and from that generate a logical index that selects the desired rows:
result = data(~any(triu(bsxfun(#lt, data(:,2).', data(:,2)))), :);

How can I display a large matrix without the word "Columns" appearing?

I want to display a large matrix, but I don't like the words "Columns x to y" to show. How can I do this?
You can use the function NUM2STR to format a large 2-D matrix A into a character array and display that. For example:
>> A = magic(15); %# This would likely break up columns when displayed
>> num2str(A) %# This won't
ans =
122 139 156 173 190 207 224 1 18 35 52 69 86 103 120
138 155 172 189 206 223 15 17 34 51 68 85 102 119 121
154 171 188 205 222 14 16 33 50 67 84 101 118 135 137
170 187 204 221 13 30 32 49 66 83 100 117 134 136 153
186 203 220 12 29 31 48 65 82 99 116 133 150 152 169
202 219 11 28 45 47 64 81 98 115 132 149 151 168 185
218 10 27 44 46 63 80 97 114 131 148 165 167 184 201
9 26 43 60 62 79 96 113 130 147 164 166 183 200 217
25 42 59 61 78 95 112 129 146 163 180 182 199 216 8
41 58 75 77 94 111 128 145 162 179 181 198 215 7 24
57 74 76 93 110 127 144 161 178 195 197 214 6 23 40
73 90 92 109 126 143 160 177 194 196 213 5 22 39 56
89 91 108 125 142 159 176 193 210 212 4 21 38 55 72
105 107 124 141 158 175 192 209 211 3 20 37 54 71 88
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104