Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to convert a matrix which has a structure like this:

{{"A", 10, "D", 1},{"B", 3, "A", 2},{"C", 7, "B", 3},{"D", 6, "C", 4}}//MatrixForm//Print;

into one that looks like this:

{{"A", 10, "A", 2},{"B", 3, "B", 3},{"C", 7, "C", 4},{"D", 6, "D", 1}}//MatrixForm//Print;

Is there a way to do this that still works when column 1 is not in alphabetical order.

share|improve this question
3  
mat[[All, 3]] = mat[[All, 1]] If you know Matlab, then Mathematica is very much like Matlab for matrix manipulations really. in Matlab the above is mat(:,3)=mat(:,1). In Mathematica, use All in place of : and [[ ]] in place of ( ), that is pretty much it. – Nasser Dec 11 '12 at 9:11
2  
You can remove // Print; at the end - still get same visual. – Vitaliy Kaurov Dec 11 '12 at 9:15
@NasserM.Abbasi that should be an answer – Rojo Dec 11 '12 at 9:24
@Rojo, Ok, please give me a minute, I need to add more to it to make useful. I do not like to make one line answers, I need to figure what more to add :) – Nasser Dec 11 '12 at 9:25
Welcome to Mathematica.SE! Please consider registering your account, then you will earn site reputation and be able to do more on the site (post graphics, edit things, etc). Learn to format. After posting a question stay around for a little while, to answer questions raised by commenters. This will streamline the Q&A process considerably. – Vitaliy Kaurov Dec 11 '12 at 9:31

1 Answer

up vote 4 down vote accepted

Despite saying

Coloumn 3 should look like coloumn 1

it seems your rearrangement from your first list to the second is much more than that because you are moving both the third and fourth elements. In other words you are not simply making column 3 the same as column 1. So therefore you need to rearrange while keeping the 3rd and 4th elements grouped together:

list = {{"A", 10, "D", 1}, {"B", 3, "A", 2}, {"C", 7, "B", 3}, {"D", 
   6, "C", 4}};
list[[All, {3, 4}]] = SortBy[list[[All, {3, 4}]], First];
list

(* {{"A", 10, "A", 2}, {"B", 3, "B", 3}, {"C", 7, "C", 4}, {"D", 6, "D", 
  1}} *)

enter image description here

Edit

Okay let's consider a case in which column 1 is not in alphabetical order.

list = {{"D", 10, "C", 1}, {"B", 3, "A", 2}, {"C", 7, "B", 3}, {"A", 
    6, "D", 4}};

Based on your example the transformed list should be

{{"D", 10, "D", 4}, {"B", 3, "B", 3}, {"C", 7, "C", 1}, {"A", 6, "A", 
  2}}

...right?

In this case you collect the order of your first column and use that later:

order = Ordering[list[[All, 1]]]
(*  {4, 2, 3, 1}  *)

Then proceed as above by sorting the grouped columns 3 and 4 and then arrange that sorted list according to the order of column 1.

list[[All, {3, 4}]] = SortBy[list[[All, {3, 4}]], First][[order]];
list

(* {{"D", 10, "D", 4}, {"B", 3, "B", 3}, {"C", 7, "C", 1}, {"A", 6, "A", 
  2}} *)

Alternatively the final step could be written as

list[[order, {3, 4}]] = SortBy[list[[All, {3, 4}]], First];
list

enter image description here

share|improve this answer
Attentive, I hadn't realised +1 – Rojo Dec 11 '12 at 9:55
+1 good observation. I also did not notice the 4th column has changed as well. I just read Coloumn 3 should look like coloumn 1 and stopped there :) – Nasser Dec 11 '12 at 10:47
thx for the nice answer. sorry that I didn't mention that column 4 should change as well – Frink Dec 11 '12 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.