how to swap rows between matrix?

2 ビュー (過去 30 日間)
sara
sara 2014 年 10 月 24 日
コメント済み: dpb 2014 年 10 月 25 日
hi guys,,, i have two matrix A & B, and i want to swap the rows between these matrix according to their Distance. the swap should be from B to A.it swap A rows with lower distance with B rows which have higher Distance. like row no.2 has D=14 and row 2 in B has D=16, so B_row should be swapped with A_row. how to do this? the matrix is below. help needed guys.
  2 件のコメント
Adam
Adam 2014 年 10 月 24 日
編集済み: Adam 2014 年 10 月 24 日
I'm not quite sure I fully understand the extent of the problem here. Can you give a full example of expected output?
e.g. I understand you want to swap row 2 of A with row 2 of B based on the distance in each, but that brings a few questions (and probably others):
  • What is your algorithm for going down all rows of the matrix? Can row n of A only be swapped with row n of B based on comparing their distance or do you want to end with all the distances in B being shorter than all those in A or something else?
  • As I understand it D is the distance from the 1st row of the matrix as a simple number of bits that are different. However, if you swap a row from B into A then wouldn't its distance then be measured against the top row of A rather than the top row of B which gives it its current distance?
I don't think I have time to consider a complete answer myself, but hopefully clarification on my questions will help someone else provide you with more of an answer.
sara
sara 2014 年 10 月 24 日
this algorithm should run for all the rows comparing their distance, if the distance of row n of A is shorter than row n of B, then it should swap them, and swap all the rows according to this condition.

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

採用された回答

dpb
dpb 2014 年 10 月 24 日
Presuming you mean literally the simple-minded swap row-by-row depending on the D between the two, it's pretty simple.
Given A and B,
ixless=A(:,2)<B(:,2); % the offending rows
C=A; % need temporary copy
A(ixless,:)=B(ixless,:); % move needed elements of B into A
B(ixless,:)=C(ixless,:); % ditto to put A into B (C is still original A)
clear C
  12 件のコメント
sara
sara 2014 年 10 月 25 日
this is good,but how to get the output as matrix (with the swapped rows)?
dpb
dpb 2014 年 10 月 25 日
What you mean? The above uses whatever matrices A and B are; substitute variable names appropriately for your case.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by