Hi,
I am trying to re-arrange a square matrix of any order to the format given below.
I tried to work on a looping method, considering the the matrix index j as starting from 2 and ending at the maximum number of rows (or columns) in the original matrix, while the the index i would always start at 1 and increase till the value of j. Also, within a single loop, the index j would decrease from the initial value to 1. The code I tried is below,
for j=2:6;
xq=x(1:j,j:-1:1);
end
While I expected the indices i and j to change simultaneously, I understood that this behaves like a nested for loop. Do we have some method to increment and decrement the indices simultaneously? Else, is there any other elegant way of approaching this problem? I appreciate your help.
Thanks in advance.

댓글 수: 3

Jan
Jan 2017년 7월 18일
You expect i to change? But it is set to 1 and does not change its value anywhere. I do not see aynthing, which bahaves like a nested for loop.
I do not understand the wanted procedure. Please post an example created manually.
dpb
dpb 2017년 7월 18일
There is a figure that shows the output attached, Jan, but I couldn't figure out the rule from it -- the rule for the boundary conditions doesn't seem consistent (or the example is just wrong for a consistent BC wouldn't be out of the realm of possibility).
Tailman
Tailman 2017년 7월 19일
Hi Jan and dpb,
I apologize for not being clear enough. I should have deleted the i given there(I'll do it now). What I meant by 'i' was, the row index. Even if I could get multiple row matrices like [12, 21], [13,22,31], [14,23,32,41] and so on up till the highest row (i.e., 1,j, 2,j-1,.., j,1) value of any given square matrix, that is what I want to achieve.
dpb,
The matrix does have a rule to it. If you look at the resultant matrix which I want, by removing only the first column, we will get a symmetric matrix, but, I am unable to store the values in that specific order. Is that achievable?
I am grateful for your replies. Thank you.

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 7월 19일
편집: Andrei Bobrov 2017년 7월 19일

1 개 추천

omtx = 10*(1:6)' + (1:6); % your original matrix
m = size(omtx,1);
y = tril(true(m));
a = omtx.';
fmtx = y + 0;
fmtx(y) = a(rot90(y,-1));
fmtx = fmtx + tril(fmtx,-1)';

댓글 수: 3

Tailman
Tailman 2017년 7월 19일
Hello Andrei,
Thank you for the answer. That solved my problem. :)
Tailman
Tailman 2017년 7월 19일
편집: Tailman 2017년 7월 19일
Andrei,
I used the original answer, i.e., I just used the original matrix in place of omtx and it seems to work. Is the first line of this code just a sample? Thanks again.
P.S. I'm wondering how MATLAB adds two matrices of different dimensions.
Andrei Bobrov
Andrei Bobrov 2017년 7월 19일
yes, just a sample.
For your PS: read here.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 7월 18일

댓글:

2017년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by