How to copy a matrix but skipping some row and column?

6 views (last 30 days)
So i have a 24X24 matrix and i want to copy it into another matrix but skipping some row and column.
For example, I want to copy all value but skip both row and column of 1, 2,4, 6, 8, 17 and 19 into a new matrix with new dimension of 17x17.

Accepted Answer

Star Strider
Star Strider on 25 Jun 2022
A = randi(99,24)
A = 24×24
16 35 93 11 14 41 30 44 95 22 94 7 51 84 13 79 42 39 25 82 16 13 35 39 98 73 63 86 55 59 58 42 56 12 29 52 53 41 56 46 33 11 8 26 68 89 81 31 1 20 59 75 32 55 96 74 44 95 77 70 38 22 55 6 25 8 32 23 10 99 36 9 25 53 25 40 60 18 55 44 81 83 76 4 21 4 74 32 28 3 53 80 46 22 22 15 88 26 13 39 39 19 51 7 68 66 42 27 69 99 87 82 97 29 27 55 82 14 51 85 49 16 39 25 25 30 53 36 3 66 6 89 79 16 10 8 40 29 92 74 82 92 71 76 5 48 89 79 65 17 94 35 37 15 17 80 90 13 59 13 3 76 26 25 29 91 4 84 60 8 49 97 12 56 30 35 66 75 11 78 41 78 26 6 99 55 89 80 54 21 81 14 69 83 16 75 2 4 32 86 49 13 73 27 82 2 91 77 89 65 3 91 57 53 95 23 74 70 89 86 18 13 69 24 99 72 91 51 72 70 6 62 80 7 15 4 18 47 72 15
skiprc = [1, 2,4, 6, 8, 17, 19];
copyrc = setdiff(1:24, skiprc)
copyrc = 1×17
3 5 7 9 10 11 12 13 14 15 16 18 20 21 22 23 24
B = A(copyrc, copyrc)
B = 17×17
59 32 96 44 95 77 70 38 22 55 6 8 23 10 99 36 9 13 39 51 68 66 42 27 69 99 87 82 29 55 82 14 51 85 89 65 94 37 15 17 80 90 13 59 13 76 25 29 91 4 84 16 2 32 49 13 73 27 82 2 91 77 65 91 57 53 95 23 89 18 69 99 72 91 51 72 70 6 62 7 4 18 47 72 15 85 39 73 58 9 98 84 23 91 51 15 61 29 19 25 80 46 53 86 18 94 17 40 31 17 69 51 52 64 29 4 3 83 19 43 85 60 17 72 82 61 27 46 80 76 73 80 93 58 16 42 29 22 27 80 7 63 64 76 58 15 48 20 93 94 76 86 69 36 28 99 16 84 98 35 82 50 14 21 15 46 32 87 53 30
As requested!
.

More Answers (2)

Walter Roberson
Walter Roberson on 25 Jun 2022
M = rand(24, 24);
toskip = [1, 2, 4, 6, 8, 17, 19];
%version 1
M1 = M; M1(toskip, :) = []; M1(:, toskip) = [];
size(M1)
ans = 1×2
17 17
%version 2
tokeep = setdiff(1:size(M,1), toskip);
M2 = M(tokeep, tokeep);
size(M2)
ans = 1×2
17 17
isequal(M1, M2)
ans = logical
1

Voss
Voss on 25 Jun 2022
A = reshape(1:24^2,24,[])
A = 24×24
1 25 49 73 97 121 145 169 193 217 241 265 289 313 337 361 385 409 433 457 481 505 529 553 2 26 50 74 98 122 146 170 194 218 242 266 290 314 338 362 386 410 434 458 482 506 530 554 3 27 51 75 99 123 147 171 195 219 243 267 291 315 339 363 387 411 435 459 483 507 531 555 4 28 52 76 100 124 148 172 196 220 244 268 292 316 340 364 388 412 436 460 484 508 532 556 5 29 53 77 101 125 149 173 197 221 245 269 293 317 341 365 389 413 437 461 485 509 533 557 6 30 54 78 102 126 150 174 198 222 246 270 294 318 342 366 390 414 438 462 486 510 534 558 7 31 55 79 103 127 151 175 199 223 247 271 295 319 343 367 391 415 439 463 487 511 535 559 8 32 56 80 104 128 152 176 200 224 248 272 296 320 344 368 392 416 440 464 488 512 536 560 9 33 57 81 105 129 153 177 201 225 249 273 297 321 345 369 393 417 441 465 489 513 537 561 10 34 58 82 106 130 154 178 202 226 250 274 298 322 346 370 394 418 442 466 490 514 538 562
to_skip = [1, 2, 4, 6, 8, 17, 19];
% one way
B = A; % 'copy', then
B(to_skip,:) = []; % remove those rows
B(:,to_skip) = [] % and columns
B = 17×17
51 99 147 195 219 243 267 291 315 339 363 411 459 483 507 531 555 53 101 149 197 221 245 269 293 317 341 365 413 461 485 509 533 557 55 103 151 199 223 247 271 295 319 343 367 415 463 487 511 535 559 57 105 153 201 225 249 273 297 321 345 369 417 465 489 513 537 561 58 106 154 202 226 250 274 298 322 346 370 418 466 490 514 538 562 59 107 155 203 227 251 275 299 323 347 371 419 467 491 515 539 563 60 108 156 204 228 252 276 300 324 348 372 420 468 492 516 540 564 61 109 157 205 229 253 277 301 325 349 373 421 469 493 517 541 565 62 110 158 206 230 254 278 302 326 350 374 422 470 494 518 542 566 63 111 159 207 231 255 279 303 327 351 375 423 471 495 519 543 567
% another way
idx = ~ismember(1:24,to_skip); % idx "to keep"
C = A(idx,idx)
C = 17×17
51 99 147 195 219 243 267 291 315 339 363 411 459 483 507 531 555 53 101 149 197 221 245 269 293 317 341 365 413 461 485 509 533 557 55 103 151 199 223 247 271 295 319 343 367 415 463 487 511 535 559 57 105 153 201 225 249 273 297 321 345 369 417 465 489 513 537 561 58 106 154 202 226 250 274 298 322 346 370 418 466 490 514 538 562 59 107 155 203 227 251 275 299 323 347 371 419 467 491 515 539 563 60 108 156 204 228 252 276 300 324 348 372 420 468 492 516 540 564 61 109 157 205 229 253 277 301 325 349 373 421 469 493 517 541 565 62 110 158 206 230 254 278 302 326 350 374 422 470 494 518 542 566 63 111 159 207 231 255 279 303 327 351 375 423 471 495 519 543 567

Community Treasure Hunt

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

Start Hunting!

Translated by