Replacing sequences in matrix
1 次查看(过去 30 天)
显示 更早的评论
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';
0 个评论
采纳的回答
Stephen23
2023-5-15
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]
B = sortrows(A,-1)
2 个评论
Stephen23
2023-5-15
编辑:Stephen23
2023-5-15
"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].'
B = reshape(sortrows(reshape(A,10,20),-1),100,2)
更多回答(2 个)
Shaik
2023-5-15
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);
2 个评论
Shaik
2023-5-15
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);
VBBV
2023-5-15
编辑:VBBV
2023-5-15
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 = fliplr(reshape(sort(results.','descend'),10,[]).')
2 个评论
VBBV
2023-5-15
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 = fliplr(reshape(sort(results.','descend'),10,[]).')
%
results = reshape(results.',[],2)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!