Swapping the elements in a matrix

11 ビュー (過去 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 件のコメント
Peterikye
Peterikye 2017 年 8 月 28 日
Thanks, it worked.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by