Swapping the elements in a matrix

조회 수: 14 (최근 30일)
Peterikye
Peterikye 2017년 8월 28일
댓글: Peterikye 2017년 8월 28일
Hi everyone, I have a matrix :
[0.8 0.7 0.9 0.5;0.6 0.4 0.9 0.7;0.8 0.5 0.6 0.5;0.8 0.6 0.5 0.4]
I want to write a nested for loop to swap the minimum element and maximum element of two direct rows until the minimum element in each row is greater than or equal to maximum element in the subsequent row. Such that the final matrix looks like this:
[0.8 0.8 0.9 0.9;0.7 0.8 0.6 0.7;0.6 0.6 0.5 0.5;0.5 0.4 0.5 0.4]
Thanks
  댓글 수: 2
James Tursa
James Tursa 2017년 8월 28일
편집: James Tursa 2017년 8월 28일
What is your definition of "two direct rows"? I.e., exactly which rows are being compared for the swapping?
Also, the location of the numbers in the end result is going to depend on the order in which you do the row comparisons. So you will need to specify precisely how you want this done. E.g., you could compare two rows and do swapping on them until they met the criteria, and then move on. Or you could do only one swap and move on. In either case coming back to them later to see if there are any additional swaps that must be done. You will get different final answers depending on what order you do the row comparisons, and depending on which elements get swapped if there is more than one min or max in the row.
Peterikye
Peterikye 2017년 8월 28일
Firstly, the matrix is arranged based on the sum of the rows. If the minimum element of the first row is less than the maximum element of the second row, the positions should be swapped and the matrix should again be rearranged based on the new matrix obtained. Else if the minimum element of the first row is greater than or equal to the maximum of the second row, comparison goes the next two rows, i.e., second row and third row and the process continues until all the criteria are met. I will really appreciate your help. I wrote a code but I am finding difficult to execute it in a loop. Thanks

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

채택된 답변

James Tursa
James Tursa 2017년 8월 28일
You've got hard-coded row numbers and matrix sizes in your code. Rather than doing that, along with all of the copy-pasting you are doing, here is some code that uses looping. I think it matches your description of what you want to happen, but I don't get the result that you show above. Maybe some variation of this using a different ordering of the comparisons will yield the exact result you show above, but I have not been able to reproduce it yet.
function x = swapminmax(x)
% Sort by row sums
[~,s] = sort(sum(x,2),'descend');
x = x(s,:);
% Init looping parameters
m = size(x,1);
swap = true;
% Loop until no swapping was done
while( swap )
swap = false;
% Loop over the rows until a swap is done
for i=1:m-1
while( true ) % Do all the swaps for these two rows
[MINX,MINI] = min(x(i ,:));
[MAXX,MAXI] = max(x(i+1,:));
if( MINX >= MAXX )
break; % No swaps for these rows, so break and go to next two rows
end
% Swap the min and max elements
x(i ,MINI) = MAXX;
x(i+1,MAXI) = MINX;
swap = true;
end
end
end
end

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 8월 28일
편집: Andrei Bobrov 2017년 8월 28일
A = [0.8 0.7 0.9 0.5;0.6 0.4 0.9 0.7;0.8 0.5 0.6 0.5;0.8 0.6 0.5 0.4];
n = size(A,1);
for ii = rem(0:(n-1)^(n-1)-1,n-1)+1
[v1,k1] = min(A(ii,:));
[v2,k2] = max(A(ii+1,:));
if v1 < v2
t = A(ii,k1);
A(ii,k1) = A(ii+1,k2);
A(ii+1,k2) = t;
end
end
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2017년 8월 28일
fixed
Peterikye
Peterikye 2017년 8월 28일
Thanks, it worked

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

카테고리

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