creating a matrix by a vector in the following condition

조회 수: 1 (최근 30일)
Bahar
Bahar 2018년 2월 22일
답변: SRT HellKitty 2018년 2월 23일
Imagine that there is a vector, such as:
v=[a1; a2; a3; a4; a5; a1; a2; a3; a4; a5; a1; a2; a3; a4; a5; a1; a2; a3; a4; a5; a1; a2; a3; a4; a5;]
I need to rearrange this vector and transfer it to a matrix such that the elements are positioned like the following matrix:
matrix=
a1 a2 a3
a2 a1 a2
a3 a2 a1
a4 a3 a2
a5 a4 a3
a1 a2 a3
a2 a1 a2
a3 a2 a1
a4 a3 a2
a5 a4 a3
I need a for loop that creates this matrix. It is like each column starts with the second element of the previous column(e.g.: a2 in column 2) and ends at the one to the last element of the previous column(e.g: a3 at column 3). Also, it happens for each block (a1 to a5).
I could not explain it very well but hopefully, the example makes it clear.
Thanks in advance for your help.
  댓글 수: 2
James Tursa
James Tursa 2018년 2월 23일
편집: James Tursa 2018년 2월 23일
No, this is not clear. For one, your 3rd column does not seem to satisfy the "starts with the second element of the previous column" rule. And I can't figure out the rule for filling in the in-between elements either. Why do you have to use a for-loop?
Bahar
Bahar 2018년 2월 23일
Ok forget what I explained, please just look at the example. the first block of the matrix is:
a1 a2 a3
a2 a1 a2
a3 a2 a1
a4 a3 a2
a5 a4 a3
And the second block is the same which adds to the first one.
a1 a2 a3
a2 a1 a2
a3 a2 a1
a4 a3 a2
a5 a4 a3
a1 a2 a3
a2 a1 a2
a3 a2 a1
a4 a3 a2
a5 a4 a3
And it needs to create by the above vector.

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

답변 (1개)

SRT HellKitty
SRT HellKitty 2018년 2월 23일
If each column is always going to be the same length and 'v' is always going to be 1-5 repeated this should work.
matrix = [[v(1:10)], [v(2); v(1:9)], [v(3); v(2); v(1:8)]]

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by