Change positions and elements in a double array

조회 수: 2 (최근 30일)
Luigi
Luigi 2018년 3월 23일
댓글: Stephen23 2018년 3월 23일
Hello everybody, I have a n x n double array, in which the row i has the same elements of column i. I want to swap its elements in such a way the new array will have row i and column i in another position (i.e. j). So the resulting array will have in row j what there was in row i and in the column j what there was in column i. In the array diagunal, I have all Nan, because in this position I have the correlation between the row and the column belonging to the same element (representing correlation between a a item with itself). I tried to do a copy of the array and then to assign the right positions in the new array, but it seems that I didn't get the result I would like to have, because, for example, I didn't have NaN on the diagunal. Any help? Thank you in advance, Luigi
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2018년 3월 23일
Luigi - when doing the swap, I suppose (?) that you will need to move the NaN that used to be on the diagonal in row i to the new diagonal position when swapped with row j. But how? For example, if I am swapping my first and second rows (and first and second columns) then
A = [NaN 2 3 4 5;
2 NaN 4 5 1;
3 4 NaN 1 2;
4 5 1 NaN 3;
5 1 2 3 NaN];
Then swapping the first and second rows and columns gives us
2 NaN 4 5 1
NaN 2 3 4 5
4 3 NaN 1 2
5 4 1 NaN 3
1 5 2 3 NaN
What is your rule for restoring the NaN to the diagonal? In this case, do we just swap NaN with 2?
Stephen23
Stephen23 2018년 3월 23일
Luigi's "Answer" moved here:
Thank you so much Geoff for your help. That's exactly the problem I have. The rule is to have exactly the same values in the arrays but in different positions (if the correlation between i and j is 1, this value should be the same when i changed positions to element i and j. Another rule, which I don't know if it is clear is that correlation(i,j) = correlation (j,i)). The only rule about NaN is to have on the diagonal all Nan, but I'm realizing that probably this is not possible, because the rule to have NaNs on the diagonal imply to change, after the swap, at least two values, the actual value where NaN should be and the value where NaN actually is and this will produce an array that will be different from the original one. My problem is that, even if I'm not able to have NaNs on the diagonal, when I do the swap I didn't find NaN in the position where there is correlation value between the same items. My approach was to create an array of zeros with the same dimensions of the original one and then to copy the rows in the original array into the right row in the new one, but it doesn't work, probably i do some stupid mistake.

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

답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by