Swapping the elements in a matrix

10 views (last 30 days)
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 Comments
James Tursa
James Tursa on 28 Aug 2017
Edited: James Tursa on 28 Aug 2017
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 on 28 Aug 2017
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

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 28 Aug 2017
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

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 28 Aug 2017
Edited: Andrei Bobrov on 28 Aug 2017
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!