How to reshape any matrix using while loop or any other method?

1 view (last 30 days)
Hello there, I have a matrix B of size 432000x120 and I want another matrix A of same size in such a way that:
A(: , 1) = B(: , 1)
A(: , 2) = B(: , 7)
A(: , 3) = B(: , 13) and so on
I have done by using two for loops but I wanted to solve this problem by other method (may be by using while loop or any other efficient method). You help will be greatly appreciated.

Accepted Answer

Voss
Voss on 29 Jun 2022
Here's one way, based on the assumption that it goes
A(:,[1,2,3,...,20,21,22,...]) = B(:,[1,7,13,...,115,2,8,...])
% 5x120 matrix B:
B = reshape(1:5*120,120,[]).'
B = 5×120
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
n = 6;
% construct A by reordering the columns of B:
A = B(:,(1:n)+(0:n:size(B,2)-1).')
A = 5×120
1 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97 103 109 115 2 8 14 20 26 32 38 44 50 56 121 127 133 139 145 151 157 163 169 175 181 187 193 199 205 211 217 223 229 235 122 128 134 140 146 152 158 164 170 176 241 247 253 259 265 271 277 283 289 295 301 307 313 319 325 331 337 343 349 355 242 248 254 260 266 272 278 284 290 296 361 367 373 379 385 391 397 403 409 415 421 427 433 439 445 451 457 463 469 475 362 368 374 380 386 392 398 404 410 416 481 487 493 499 505 511 517 523 529 535 541 547 553 559 565 571 577 583 589 595 482 488 494 500 506 512 518 524 530 536
% another way to do the same reordering:
A = B(:,reshape(1:size(B,2),n,[]).')
A = 5×120
1 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97 103 109 115 2 8 14 20 26 32 38 44 50 56 121 127 133 139 145 151 157 163 169 175 181 187 193 199 205 211 217 223 229 235 122 128 134 140 146 152 158 164 170 176 241 247 253 259 265 271 277 283 289 295 301 307 313 319 325 331 337 343 349 355 242 248 254 260 266 272 278 284 290 296 361 367 373 379 385 391 397 403 409 415 421 427 433 439 445 451 457 463 469 475 362 368 374 380 386 392 398 404 410 416 481 487 493 499 505 511 517 523 529 535 541 547 553 559 565 571 577 583 589 595 482 488 494 500 506 512 518 524 530 536
% check first and last column of each 20-column sequence:
isequal( ...
A(:,[1 20 21 40 41 60 61 80 81 100 101 120]), ...
B(:,[1 115 2 116 3 117 4 118 5 119 6 120]))
ans = logical
1
  1 Comment
Sushil Pokharel
Sushil Pokharel on 30 Jun 2022
hey @Voss, as always thank you so much and also I learned a lot from you guys thank you for that as well.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 29 Jun 2022
A = B(:, 1:6:end) ;
  5 Comments

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by