Replacing sequences in matrix

9 views (last 30 days)
Smithy
Smithy on 15 May 2023
Edited: Stephen23 on 15 May 2023
Hello everybody,
I have a matirx and I want to change the order of matrix in accordance with first column.
First column is the index start from 4 to 13 and others an offset at 100 intervals from 4 to 13.
Now it is from 4...13,104..113.. to 913. I would like to make it from 904...913,804..814.. to 13.
Is there a way to make the matrix in such an order.
clear; close all; clc;
results = [4,5,6,7,8,9,10,11,12,13, ...
104,105,106,107,108,109,110,111,112,113, ...
204,205,206,207,208,209,210,211,212,213, ...
304,305,306,307,308,309,310,311,312,313, ...
404,405,406,407,408,409,410,411,412,413, ...
504,505,506,507,508,509,510,511,512,513, ...
604,605,606,607,608,609,610,611,612,613, ...
704,705,706,707,708,709,710,711,712,713, ...
804,805,806,807,808,809,810,811,812,813, ...
904,905,906,907,908,909,910,911,912,913; ...
16 79 31 53 17 60 26 65 69 75 45 8 23 91 15 83 54 100 8 44 11 96 0 77 82 87 8 40 26 80 43 91 18 26 15 14 87 58 55 14 85 62 35 51 40 8 24 12 18 24 42 5 90 94 49 49 34 90 37 11 78 39 24 40 10 13 94 96 58 6 23 35 82 2 4 17 65 73 65 45 55 30 74 19 69 18 37 63 78 8 93 78 49 44 45 31 51 51 82 79];
results = results';

Accepted Answer

Stephen23
Stephen23 on 15 May 2023
A = [4,5,6,7,8,9,10,11,12,13; 104,105,106,107,108,109,110,111,112,113; 204,205,206,207,208,209,210,211,212,213; 304,305,306,307,308,309,310,311,312,313; 404,405,406,407,408,409,410,411,412,413; 504,505,506,507,508,509,510,511,512,513; 604,605,606,607,608,609,610,611,612,613; 704,705,706,707,708,709,710,711,712,713; 804,805,806,807,808,809,810,811,812,813; 904,905,906,907,908,909,910,911,912,913]
A = 10×10
4 5 6 7 8 9 10 11 12 13 104 105 106 107 108 109 110 111 112 113 204 205 206 207 208 209 210 211 212 213 304 305 306 307 308 309 310 311 312 313 404 405 406 407 408 409 410 411 412 413 504 505 506 507 508 509 510 511 512 513 604 605 606 607 608 609 610 611 612 613 704 705 706 707 708 709 710 711 712 713 804 805 806 807 808 809 810 811 812 813 904 905 906 907 908 909 910 911 912 913
B = sortrows(A,-1)
B = 10×10
904 905 906 907 908 909 910 911 912 913 804 805 806 807 808 809 810 811 812 813 704 705 706 707 708 709 710 711 712 713 604 605 606 607 608 609 610 611 612 613 504 505 506 507 508 509 510 511 512 513 404 405 406 407 408 409 410 411 412 413 304 305 306 307 308 309 310 311 312 313 204 205 206 207 208 209 210 211 212 213 104 105 106 107 108 109 110 111 112 113 4 5 6 7 8 9 10 11 12 13
  2 Comments
Smithy
Smithy on 15 May 2023
Edited: Smithy on 15 May 2023
Thank you very much. I really appreciate with it. The result variable is the 100 by 2 matrix. It is possible to make the order including with the 2nd column together?
Stephen23
Stephen23 on 15 May 2023
Edited: Stephen23 on 15 May 2023
"It is possible to make the order including with the 2nd column together?"
Perhaps this does what you want:
A = [4,5,6,7,8,9,10,11,12,13,104,105,106,107,108,109,110,111,112,113,204,205,206,207,208,209,210,211,212,213,304,305,306,307,308,309,310,311,312,313,404,405,406,407,408,409,410,411,412,413,504,505,506,507,508,509,510,511,512,513,604,605,606,607,608,609,610,611,612,613,704,705,706,707,708,709,710,711,712,713,804,805,806,807,808,809,810,811,812,813,904,905,906,907,908,909,910,911,912,913;16,79,31,53,17,60,26,65,69,75,45,8,23,91,15,83,54,100,8,44,11,96,0,77,82,87,8,40,26,80,43,91,18,26,15,14,87,58,55,14,85,62,35,51,40,8,24,12,18,24,42,5,90,94,49,49,34,90,37,11,78,39,24,40,10,13,94,96,58,6,23,35,82,2,4,17,65,73,65,45,55,30,74,19,69,18,37,63,78,8,93,78,49,44,45,31,51,51,82,79].'
A = 100×2
4 16 5 79 6 31 7 53 8 17 9 60 10 26 11 65 12 69 13 75
B = reshape(sortrows(reshape(A,10,20),-1),100,2)
B = 100×2
13 75 12 69 11 65 10 26 9 60 8 17 7 53 6 31 5 79 4 16

Sign in to comment.

More Answers (2)

Shaik
Shaik on 15 May 2023
Hi Smithy,
Can you check this once, hope it helps
clear; close all; clc;
results = [4,5,6,7,8,9,10,11,12,13, ...
104,105,106,107,108,109,110,111,112,113, ...
204,205,206,207,208,209,210,211,212,213, ...
304,305,306,307,308,309,310,311,312,313, ...
404,405,406,407,408,409,410,411,412,413, ...
504,505,506,507,508,509,510,511,512,513, ...
604,605,606,607,608,609,610,611,612,613, ...
704,705,706,707,708,709,710,711,712,713, ...
804,805,806,807,808,809,810,811,812,813, ...
904,905,906,907,908,909,910,911,912,913; ...
16 79 31 53 17 60 26 65 69 75 45 8 23 91 15 83 54 100 8 44 11 96 0 77 82 87 8 40 26 80 43 91 18 26 15 14 87 58 55 14 85 62 35 51 40 8 24 12 18 24 42 5 90 94 49 49 34 90 37 11 78 39 24 40 10 13 94 96 58 6 23 35 82 2 4 17 65 73 65 45 55 30 74 19 69 18 37 63 78 8 93 78 49 44 45 31 51 51 82 79];
results = results';
% Extract the first column
first_column = results(:, 1);
% Sort the matrix based on the first column
sorted_results = sortrows(results, 1);
% Display the sorted matrix
disp(sorted_results);
4 16 5 79 6 31 7 53 8 17 9 60 10 26 11 65 12 69 13 75 104 45 105 8 106 23 107 91 108 15 109 83 110 54 111 100 112 8 113 44 204 11 205 96 206 0 207 77 208 82 209 87 210 8 211 40 212 26 213 80 304 43 305 91 306 18 307 26 308 15 309 14 310 87 311 58 312 55 313 14 404 85 405 62 406 35 407 51 408 40 409 8 410 24 411 12 412 18 413 24 504 42 505 5 506 90 507 94 508 49 509 49 510 34 511 90 512 37 513 11 604 78 605 39 606 24 607 40 608 10 609 13 610 94 611 96 612 58 613 6 704 23 705 35 706 82 707 2 708 4 709 17 710 65 711 73 712 65 713 45 804 55 805 30 806 74 807 19 808 69 809 18 810 37 811 63 812 78 813 8 904 93 905 78 906 49 907 44 908 45 909 31 910 51 911 51 912 82 913 79
  2 Comments
Smithy
Smithy on 15 May 2023
Edited: Smithy on 15 May 2023
Thank you very much for your huge helps. Is there a way to make the sequence as the below? 904,905,906,907,908,909,910,911,912,913,804,805,806,807,808,809,810,811,812,813,
704,705,706,707,708,709,710,711,712,713, ... , 104,105,106,107,108,109,110,111,112,113,
4,5,6,7,8,9,10,11,12,13
Shaik
Shaik on 15 May 2023
Certainly! To reorder the matrix as you specified, you can use the following code:
clear; close all; clc;
results = [4,5,6,7,8,9,10,11,12,13, ...
104,105,106,107,108,109,110,111,112,113, ...
204,205,206,207,208,209,210,211,212,213, ...
304,305,306,307,308,309,310,311,312,313, ...
404,405,406,407,408,409,410,411,412,413, ...
504,505,506,507,508,509,510,511,512,513, ...
604,605,606,607,608,609,610,611,612,613, ...
704,705,706,707,708,709,710,711,712,713, ...
804,805,806,807,808,809,810,811,812,813, ...
904,905,906,907,908,909,910,911,912,913; ...
16 79 31 53 17 60 26 65 69 75 45 8 23 91 15 83 54 100 8 44 11 96 0 77 82 87 8 40 26 80 43 91 18 26 15 14 87 58 55 14 85 62 35 51 40 8 24 12 18 24 42 5 90 94 49 49 34 90 37 11 78 39 24 40 10 13 94 96 58 6 23 35 82 2 4 17 65 73 65 45 55 30 74 19 69 18 37 63 78 8 93 78 49 44 45 31 51 51 82 79];
results = results';
% Extract the first column
first_column = results(:, 1);
% Calculate the number of blocks
num_blocks = numel(first_column) / 10;
% Reshape the matrix into blocks of 10 rows
block_matrix = reshape(results, 10, []).';
% Create an array for the desired order of blocks
block_order = [num_blocks:-1:1];
% Rearrange the blocks based on the desired order
rearranged_matrix = block_matrix(block_order, :);
% Flatten the rearranged matrix back into a single column matrix
rearranged_results = rearranged_matrix(:);
% Display the rearranged matrix
disp(rearranged_results);
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

Sign in to comment.


VBBV
VBBV on 15 May 2023
Edited: VBBV on 15 May 2023
clear; close all; clc;
results = [4 5 6 7 8,9 10 11,12,13, ...
104,105,106,107,108,109,110,111,112,113, ...
204,205,206,207,208,209,210,211,212,213, ...
304,305,306,307,308,309,310,311,312,313, ...
404,405,406,407,408,409,410,411,412,413, ...
504,505,506,507,508,509,510,511,512,513, ...
604,605,606,607,608,609,610,611,612,613, ...
704,705,706,707,708,709,710,711,712,713, ...
804,805,806,807,808,809,810,811,812,813, ...
904,905,906,907,908,909,910,911,912,913, ...
16 79 31 53 17 60 26 65 69 75 45 8 23 91 15 83 54 100 8 44 11 96 0 77 82 87 8 40 26 80 43 91 18 26 15 14 87 58 55 14 85 62 35 51 40 8 24 12 18 24 42 5 90 94 49 49 34 90 37 11 78 39 24 40 10 13 94 96 58 6 23 35 82 2 4 17 65 73 65 45 55 30 74 19 69 18 37 63 78 8 93 78 49 44 45 31 51 51 82 79]
results = 1×200
4 5 6 7 8 9 10 11 12 13 104 105 106 107 108 109 110 111 112 113 204 205 206 207 208 209 210 211 212 213
results = fliplr(reshape(sort(results.','descend'),10,[]).')
results = 20×10
904 905 906 907 908 909 910 911 912 913 804 805 806 807 808 809 810 811 812 813 704 705 706 707 708 709 710 711 712 713 604 605 606 607 608 609 610 611 612 613 504 505 506 507 508 509 510 511 512 513 404 405 406 407 408 409 410 411 412 413 304 305 306 307 308 309 310 311 312 313 204 205 206 207 208 209 210 211 212 213 104 105 106 107 108 109 110 111 112 113 90 90 91 91 93 94 94 96 96 100
  2 Comments
Smithy
Smithy on 15 May 2023
Edited: Smithy on 15 May 2023
Thank you very much. I really appreciate with it. The result variable is the 100 by 2 matrix. It is possible to make the order including with the 2nd column together?
VBBV
VBBV on 15 May 2023
Do you mean like this ?
clear; close all; clc;
results = [4 5 6 7 8,9 10 11,12,13, ...
104,105,106,107,108,109,110,111,112,113, ...
204,205,206,207,208,209,210,211,212,213, ...
304,305,306,307,308,309,310,311,312,313, ...
404,405,406,407,408,409,410,411,412,413, ...
504,505,506,507,508,509,510,511,512,513, ...
604,605,606,607,608,609,610,611,612,613, ...
704,705,706,707,708,709,710,711,712,713, ...
804,805,806,807,808,809,810,811,812,813, ...
904,905,906,907,908,909,910,911,912,913, ...
16 79 31 53 17 60 26 65 69 75 45 8 23 91 15 83 54 100 8 44 11 96 0 77 82 87 8 40 26 80 43 91 18 26 15 14 87 58 55 14 85 62 35 51 40 8 24 12 18 24 42 5 90 94 49 49 34 90 37 11 78 39 24 40 10 13 94 96 58 6 23 35 82 2 4 17 65 73 65 45 55 30 74 19 69 18 37 63 78 8 93 78 49 44 45 31 51 51 82 79]
results = 1×200
4 5 6 7 8 9 10 11 12 13 104 105 106 107 108 109 110 111 112 113 204 205 206 207 208 209 210 211 212 213
results = fliplr(reshape(sort(results.','descend'),10,[]).')
results = 20×10
904 905 906 907 908 909 910 911 912 913 804 805 806 807 808 809 810 811 812 813 704 705 706 707 708 709 710 711 712 713 604 605 606 607 608 609 610 611 612 613 504 505 506 507 508 509 510 511 512 513 404 405 406 407 408 409 410 411 412 413 304 305 306 307 308 309 310 311 312 313 204 205 206 207 208 209 210 211 212 213 104 105 106 107 108 109 110 111 112 113 90 90 91 91 93 94 94 96 96 100
%
results = reshape(results.',[],2)
results = 100×2
904 79 905 79 906 80 907 82 908 82 909 82 910 83 911 85 912 87 913 87

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!