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

5 vues (au cours des 30 derniers jours)
Sushil Pokharel
Sushil Pokharel le 29 Juin 2022
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.

Réponse acceptée

Voss
Voss le 29 Juin 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 commentaire
Sushil Pokharel
Sushil Pokharel le 30 Juin 2022
hey @Voss, as always thank you so much and also I learned a lot from you guys thank you for that as well.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 29 Juin 2022
A = B(:, 1:6:end) ;
  5 commentaires
Voss
Voss le 29 Juin 2022
Modifié(e) : Voss le 29 Juin 2022
I don't think that works correctly when the number of columns is not 6^2:
B = (1:120) + (1:5).'*100;
A = reshape(permute(reshape(B, size(B,1), [], 6), [1 3 2]), size(B,1), [])
A = 5×120
101 121 141 161 181 201 102 122 142 162 182 202 103 123 143 163 183 203 104 124 144 164 184 204 105 125 145 165 185 205 201 221 241 261 281 301 202 222 242 262 282 302 203 223 243 263 283 303 204 224 244 264 284 304 205 225 245 265 285 305 301 321 341 361 381 401 302 322 342 362 382 402 303 323 343 363 383 403 304 324 344 364 384 404 305 325 345 365 385 405 401 421 441 461 481 501 402 422 442 462 482 502 403 423 443 463 483 503 404 424 444 464 484 504 405 425 445 465 485 505 501 521 541 561 581 601 502 522 542 562 582 602 503 523 543 563 583 603 504 524 544 564 584 604 505 525 545 565 585 605
I guess it should be:
A = reshape(permute(reshape(B, size(B,1), [], size(B,2)/6), [1 3 2]), size(B,1), [])
A = 5×120
101 107 113 119 125 131 137 143 149 155 161 167 173 179 185 191 197 203 209 215 102 108 114 120 126 132 138 144 150 156 201 207 213 219 225 231 237 243 249 255 261 267 273 279 285 291 297 303 309 315 202 208 214 220 226 232 238 244 250 256 301 307 313 319 325 331 337 343 349 355 361 367 373 379 385 391 397 403 409 415 302 308 314 320 326 332 338 344 350 356 401 407 413 419 425 431 437 443 449 455 461 467 473 479 485 491 497 503 509 515 402 408 414 420 426 432 438 444 450 456 501 507 513 519 525 531 537 543 549 555 561 567 573 579 585 591 597 603 609 615 502 508 514 520 526 532 538 544 550 556
Sushil Pokharel
Sushil Pokharel le 30 Juin 2022
Modifié(e) : Sushil Pokharel le 30 Juin 2022
Yes, this one is what I expected.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by