Rearranging array based on number of steps.

조회 수: 5 (최근 30일)
Santos García Rosado
Santos García Rosado 2021년 6월 1일
댓글: Santos García Rosado 2021년 6월 1일
Hi everybody,
Let's say I have a vector such as:
A = (1:24)
And I'd like to separate this array into a 2x12 matrix where the first row will show the first 3 positions skipping the next three and so on... The second row will do the same thing but starting from the fourth position. This is the expexted output:
Output = [1 2 3 7 8 9 13 14 15 19 20 21; 4 5 6 10 11 12 16 17 18 22 23 24]
I could do this manually, but some times I'd like it to take 2 or 4 steps instead of 3, as it is in the example above.
Any ideas of how to to achieve this?
Thank you in advanced,
Santos

채택된 답변

Stephen23
Stephen23 2021년 6월 1일
편집: Stephen23 2021년 6월 1일
A = 1:24;
N = 3; % number of "steps"
R = 2; % number of rows
M = reshape(permute(reshape(A,N,R,[]),[2,1,3]),R,[])
M = 2×12
1 2 3 7 8 9 13 14 15 19 20 21 4 5 6 10 11 12 16 17 18 22 23 24
[1,2,3,7,8,9,13,14,15,19,20,21;4,5,6,10,11,12,16,17,18,22,23,24]
ans = 2×12
1 2 3 7 8 9 13 14 15 19 20 21 4 5 6 10 11 12 16 17 18 22 23 24
  댓글 수: 1
Santos García Rosado
Santos García Rosado 2021년 6월 1일
Thank you Steven! Neve thought of nesting permute with reshape that way. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by