Enter the elements of matrix in a specified location of a text file

2 views (last 30 days)
Hello,
I have a 10x10x10 matrix and each square contains a value between 1 and 2. What I want is, the first element of that matrix will be used in line 10, 2nd element in line 20, 3rd element in line 30,..........100th element in line 1000 of a text file. Is there any way to do that?
  4 Comments
MD MAHABUBUR ROHOMAN
MD MAHABUBUR ROHOMAN on 10 Dec 2022
they will be filled up likewise. element 201 in the line 8, element 301 in the line 7, element 401 in the line 6,......... element 901 in the ine 1, element 902 in the line 11, element 903 in the line 21

Sign in to comment.

Accepted Answer

Voss
Voss on 10 Dec 2022
Edited: Voss on 10 Dec 2022
% "data" would contain your data, but for
% demonstration, I use 1:1000 in order:
data = reshape(1:1000,[10 10 10]); % 10x10x10 array
% rearrange the elements of data as specified:
new_data = permute(data(:,:,end:-1:1),[3 1 2]);
% write to file, one element per line:
writematrix(new_data(:),'file.txt');
% check the result
type file.txt
901 801 701 601 501 401 301 201 101 1 902 802 702 602 502 402 302 202 102 2 903 803 703 603 503 403 303 203 103 3 904 804 704 604 504 404 304 204 104 4 905 805 705 605 505 405 305 205 105 5 906 806 706 606 506 406 306 206 106 6 907 807 707 607 507 407 307 207 107 7 908 808 708 608 508 408 308 208 108 8 909 809 709 609 509 409 309 209 109 9 910 810 710 610 510 410 310 210 110 10 911 811 711 611 511 411 311 211 111 11 912 812 712 612 512 412 312 212 112 12 913 813 713 613 513 413 313 213 113 13 914 814 714 614 514 414 314 214 114 14 915 815 715 615 515 415 315 215 115 15 916 816 716 616 516 416 316 216 116 16 917 817 717 617 517 417 317 217 117 17 918 818 718 618 518 418 318 218 118 18 919 819 719 619 519 419 319 219 119 19 920 820 720 620 520 420 320 220 120 20 921 821 721 621 521 421 321 221 121 21 922 822 722 622 522 422 322 222 122 22 923 823 723 623 523 423 323 223 123 23 924 824 724 624 524 424 324 224 124 24 925 825 725 625 525 425 325 225 125 25 926 826 726 626 526 426 326 226 126 26 927 827 727 627 527 427 327 227 127 27 928 828 728 628 528 428 328 228 128 28 929 829 729 629 529 429 329 229 129 29 930 830 730 630 530 430 330 230 130 30 931 831 731 631 531 431 331 231 131 31 932 832 732 632 532 432 332 232 132 32 933 833 733 633 533 433 333 233 133 33 934 834 734 634 534 434 334 234 134 34 935 835 735 635 535 435 335 235 135 35 936 836 736 636 536 436 336 236 136 36 937 837 737 637 537 437 337 237 137 37 938 838 738 638 538 438 338 238 138 38 939 839 739 639 539 439 339 239 139 39 940 840 740 640 540 440 340 240 140 40 941 841 741 641 541 441 341 241 141 41 942 842 742 642 542 442 342 242 142 42 943 843 743 643 543 443 343 243 143 43 944 844 744 644 544 444 344 244 144 44 945 845 745 645 545 445 345 245 145 45 946 846 746 646 546 446 346 246 146 46 947 847 747 647 547 447 347 247 147 47 948 848 748 648 548 448 348 248 148 48 949 849 749 649 549 449 349 249 149 49 950 850 750 650 550 450 350 250 150 50 951 851 751 651 551 451 351 251 151 51 952 852 752 652 552 452 352 252 152 52 953 853 753 653 553 453 353 253 153 53 954 854 754 654 554 454 354 254 154 54 955 855 755 655 555 455 355 255 155 55 956 856 756 656 556 456 356 256 156 56 957 857 757 657 557 457 357 257 157 57 958 858 758 658 558 458 358 258 158 58 959 859 759 659 559 459 359 259 159 59 960 860 760 660 560 460 360 260 160 60 961 861 761 661 561 461 361 261 161 61 962 862 762 662 562 462 362 262 162 62 963 863 763 663 563 463 363 263 163 63 964 864 764 664 564 464 364 264 164 64 965 865 765 665 565 465 365 265 165 65 966 866 766 666 566 466 366 266 166 66 967 867 767 667 567 467 367 267 167 67 968 868 768 668 568 468 368 268 168 68 969 869 769 669 569 469 369 269 169 69 970 870 770 670 570 470 370 270 170 70 971 871 771 671 571 471 371 271 171 71 972 872 772 672 572 472 372 272 172 72 973 873 773 673 573 473 373 273 173 73 974 874 774 674 574 474 374 274 174 74 975 875 775 675 575 475 375 275 175 75 976 876 776 676 576 476 376 276 176 76 977 877 777 677 577 477 377 277 177 77 978 878 778 678 578 478 378 278 178 78 979 879 779 679 579 479 379 279 179 79 980 880 780 680 580 480 380 280 180 80 981 881 781 681 581 481 381 281 181 81 982 882 782 682 582 482 382 282 182 82 983 883 783 683 583 483 383 283 183 83 984 884 784 684 584 484 384 284 184 84 985 885 785 685 585 485 385 285 185 85 986 886 786 686 586 486 386 286 186 86 987 887 787 687 587 487 387 287 187 87 988 888 788 688 588 488 388 288 188 88 989 889 789 689 589 489 389 289 189 89 990 890 790 690 590 490 390 290 190 90 991 891 791 691 591 491 391 291 191 91 992 892 792 692 592 492 392 292 192 92 993 893 793 693 593 493 393 293 193 93 994 894 794 694 594 494 394 294 194 94 995 895 795 695 595 495 395 295 195 95 996 896 796 696 596 496 396 296 196 96 997 897 797 697 597 497 397 297 197 97 998 898 798 698 598 498 398 298 198 98 999 899 799 699 599 499 399 299 199 99 1000 900 800 700 600 500 400 300 200 100
  6 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Cell 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!