Index in position 2 is invalid. Array indices must be positive integers or logical values.

4 views (last 30 days)
I am getting this error "Index in position 2 is invalid. Array indices must be positive integers or logical values." but am not sure how to fix it. The error is in line 13) B(iy,ix)=A(IX(iy,ix),IY(iy,ix));
N = 1000;
A = zeros(N);
IX = rand(N);
IX = round(N*IX);
IY = rand(N);
IY = round(N*IY);
% part a
tic
for ix=1:N
for iy=1:N
B(iy,ix)=A(IX(iy,ix),IY(iy,ix));
end
end
toc

Accepted Answer

Voss
Voss on 7 Apr 2022
This error happens because some element of IY is zero (could've just as easily been IX). That happens because round(N*IY) returns zero for some elements, i.e., some random numbers returned by rand are less than 1/(2*N) so round(N*rand) goes to 0.
You can make rand work for getting (valid) random indices, but it is probably easier to use randi (random integers):
N = 1000;
A = zeros(N);
IX = randi(N,[N N]) % an N-by-N matrix of random integers between 1 and N, inclusive
IX = 1000×1000
804 202 884 698 205 706 743 927 840 217 39 571 340 354 79 993 131 321 450 381 618 196 60 515 230 206 462 131 576 135 755 833 34 702 349 103 988 229 696 502 731 626 463 215 5 45 267 614 124 309 558 744 56 991 890 405 955 946 30 488 612 496 183 19 695 559 599 557 94 879 14 104 14 879 212 323 518 833 465 728 569 388 948 749 12 253 68 898 841 64 864 10 12 120 847 699 583 756 25 791 3 80 60 594 886 947 216 985 774 869 709 441 815 593 914 747 652 915 952 485 37 208 39 311 635 762 572 747 47 357 173 357 458 763 866 902 937 257 414 181 210 524 173 342 616 320 395 123 296 717 941 137 339 743 127 867 885 608 875 630 175 971 702 287 432 253 318 250 614 555 875 409 866 845 920 766 506 744 53 129 186 635 468 397 565 882 459 726 976 948 174 289 99 728 319 997 842 647 990 467 306 36 32 939 710 753 925 915 692 46 809 197 654 21 704 724 898 777 500 479 641 92 105 378 523 902 9 164 625 473 434 255 127 378 564 46 29 781 106 729 880 956 122 873 841 674 518 722 209 316 849 146 651 663 158 556 534 667 938 113 258 613 418 260 286 739 576 172 678 504 84 685 640 768 914 962 370 115 24 141 915 109 232 414 860 850 473 71 480 125 808 297 724 225 429 6 467 347 440 885
IY = randi(N,N) % different syntax for the same thing
IY = 1000×1000
367 897 172 50 232 929 705 259 789 963 633 205 735 363 695 619 510 89 949 746 347 523 867 324 733 844 458 641 485 30 788 349 305 518 317 635 828 790 246 753 511 438 332 856 629 304 765 859 768 996 213 972 361 143 708 891 836 647 941 813 891 785 425 644 236 655 442 319 491 233 253 24 354 994 961 170 338 10 58 379 651 743 187 513 160 910 872 261 834 300 160 980 259 677 89 208 994 789 499 681 14 793 524 783 319 792 670 227 321 973 243 991 283 897 76 457 719 443 423 970 341 411 176 613 919 436 856 392 427 61 19 624 718 311 257 828 556 398 334 187 355 953 232 537 744 303 828 466 743 655 5 36 241 517 541 309 95 577 923 921 472 23 599 482 450 749 191 823 780 758 127 23 57 899 890 801 608 300 315 977 987 781 413 53 826 738 226 720 255 877 499 805 740 362 551 361 962 877 247 154 715 949 869 32 150 247 426 385 382 71 865 213 88 360 192 959 589 165 323 846 876 549 252 420 535 299 271 78 784 150 459 571 44 994 402 753 445 642 388 898 810 4 348 107 292 860 880 851 493 865 618 344 882 1000 830 953 61 229 629 740 722 111 301 90 536 605 306 361 274 725 539 533 759 427 641 187 2 61 924 61 675 791 400 610 312 223 391 675 533 530 661 544 714 2 604 664 62 771 862 587
% part a
tic
for ix=1:N
for iy=1:N
B(iy,ix)=A(IX(iy,ix),IY(iy,ix));
end
end
toc
Elapsed time is 0.044904 seconds.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by