0
|
3041 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
GROUPS:

# Problems with functions: Nearest, Position

Posted 11 years ago
 I try to print index of element from vector m2, nearest to some element from vector m7.[m7 = Table[IntegerLength[7^(12 i + 1)], {i, 1, 100}]m2 = Table[IntegerLength[2^(12 i + 1)], {i, 1, 300}]For[i1 = 1, i1 < 101, i1++,  Print["  i1=", i1, "  m7=", m7[i1], " Near2=",   Nearest [m2, m7[i1]], "  Pos2=", Position[m2, m7[i1]]]]m2 is{4, 8, 12, 15, 19, 22, 26, 30, 33, 37, 41, 44, 48, 51, 55, 59, 62, 66, 69, 73, 77, 80, 84, 87, 91, 95, 98, 102, 106, 109, 113, 116, 120, 124, 127, 131, 134, 138, 142, 145, 149, 153, 156, 160, 163, 167, 171, 174, 178, 181, 185, 189, 192, 196, 199, 203, 207, 210, 214, 218, 221, 225, 228, 232, 236, 239, 243, 246, 250, 254, 257, 261, 265, 268, 272, 275, 279, 283, 286, 290, 293, 297, 301, 304, 308, 311, 315, 319, 322, 326, 330, 333, 337, 340, 344, 348, 351, 355, 358, 362, 366, 369, 373, 376, 380, 384, 387, 391, 395, 398, 402, 405, 409, 413, 416, 420, 423, 427, 431, 434, 438, 442, 445, 449, 452, 456, 460, 463, 467, 470, 474, 478, 481, 485, 488, 492, 496, 499, 503, 507, 510, 514, 517, 521, 525, 528, 532, 535, 539, 543, 546, 550, 553, 557, 561, 564, 568, 572, 575, 579, 582, 586, 590, 593, 597, 600, 604, 608, 611, 615, 619, 622, 626, 629, 633, 637, 640, 644, 647, 651, 655, 658, 662, 665, 669, 673, 676, 680, 684, 687, 691, 694, 698, 702, 705, 709, 712, 716, 720, 723, 727, 730, 734, 738, 741, 745, 749, 752, 756, 759, 763, 767, 770, 774, 777, 781, 785, 788, 792, 796, 799, 803, 806, 810, 814, 817, 821, 824, 828, 832, 835, 839, 842, 846, 850, 853, 857, 861, 864, 868, 871, 875, 879, 882, 886, 889, 893, 897, 900, 904, 908, 911, 915, 918, 922, 926, 929, 933, 936, 940, 944, 947, 951, 954, 958, 962, 965, 969, 973, 976, 980, 983, 987, 991, 994, 998, 1001, 1005, 1009, 1012, 1016, 1019}m7 is {11, 22, 32, 42, 52, 62, 72, 82, 93, 103, 113, 123, 133, 143, 153, 164, 174, 184, 194, 204, 214, 224, 235, 245, 255, 265, 275, 285, 295, 306, 316, 326, 336, 346, 356, 366, 377, 387, 397, 407, 417, 427, 437, 448, 458, 468, 478, 488, 498, 508, 519, 529, 539, 549, 559, 569, 579, 590, 600, 610, 620, 630, 640, 650, 661, 671, 681, 691, 701, 711, 721, 732, 742, 752, 762, 772, 782, 792, 802, 813, 823, 833, 843, 853, 863, 873, 884, 894, 904, 914, 924, 934, 944, 955, 965, 975, 985, 995, 1005, 1015}It's easy to see, that m2[6]=m7[2]=22, so I expect to see (when i1=2)Pos2={6} But I see i1=2  m7={...not necessary to print...} Pos2={} Another positions give the same: {} instead of {17} for element m7[6].The question: Is something wrong with my syntax of functions Nearest, Position? Or I don't understand anything else?
2 Replies
Sort By:
Posted 11 years ago
 Thanx, it works
Posted 11 years ago
 It might be a typo like using m7[ i] instead of m7[[ i]]. m7 = Table[IntegerLength[7^(12 i + 1)], {i, 1, 100}]; m2 =   Table[IntegerLength[2^(12 i + 1)], {i, 1, 300}]; For[i1 = 1, i1 < 7, i1++, a = m7[[i1]];   Print[" i1=", i1, " m7=", a, " Near2=", Nearest[m2, a], " Pos2=",    Position[m2, a]]]  (*  i1=1 m7=11 Near2={12} Pos2={}  i1=2 m7=22 Near2={22} Pos2={{6}} i1=3 m7=32 Near2={33} Pos2={} i1=4 m7=42 Near2={41} Pos2={} i1=5 m7=52 Near2={51} Pos2={} i1=6 m7=62 Near2={62} Pos2={{17}}*)