Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:40:03+00:00 2026-06-15T02:40:03+00:00

So I am sorting a matrix one row at a time in descending that

  • 0

So I am sorting a matrix one row at a time in descending that is 130X130, and I want to create a new matrix where the row name is the same, but the each sorted row column names are where the data was and the data in parenthesis next to the according column name. Its kind of like creating a psuedo3D array of the dimension 130x130x2 and then condensing it into a 130×130 matrix without column names. Here’s a smaller example.

Example

        A   B   C   D   
    A   14  82  18  50
    B   39  95  27  19
    C   60  40  32  15
    D   70  31  69  31

This is what I want

    A   B(82)   D(50)   C(18)   A(14)
    B   B(95)   A(39)   C(27)   D(19)
    C   A(60)   B(40)   C(32)   D(15)
    D   A(70)   C(69)   B(31)   D(31)

I hope this makes sense!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:40:04+00:00Added an answer on June 15, 2026 at 2:40 am

    Here you go:

    First, recreate your data:

    x <- read.table(text="
            A   B   C   D   
        A   14  82  18  50
        B   39  95  27  19
        C   60  40  32  15
        D   70  31  69  31", header=TRUE)
    

    Two apply()s, a paste() and a matrix(), and the rest is details:

    o <- apply(x, 1, order, decreasing=TRUE)
    v <- apply(x, 1, sort, decreasing=TRUE)
    
    matrix(paste(names(x)[o], t(v)), ncol=4, byrow=TRUE)
    
         [,1]   [,2]   [,3]   [,4]  
    [1,] "B 82" "D 50" "C 18" "A 14"
    [2,] "B 95" "A 39" "C 27" "D 19"
    [3,] "A 60" "B 40" "C 32" "D 15"
    [4,] "A 70" "C 69" "B 31" "D 31"
    

    EDIT: Based on a comment by Jeff Allen, this can be further simplified to:

    t(apply(x, 1, function(x){s <- order(x, decreasing=TRUE); paste(names(x)[s], x[s])}))
    
      [,1]   [,2]   [,3]   [,4]  
    A "B 82" "D 50" "C 18" "A 14"
    B "B 95" "A 39" "C 27" "D 19"
    C "A 60" "B 40" "C 32" "D 15"
    D "A 70" "C 69" "B 31" "D 31"
    

    (Since this has only one apply it should be even faster.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble sorting two related but separate lists of tuple lists. One list
i want to sorting by descending order based on date.i dont know how to
I'm sorting a NSMutableArray that has NSString that contain numbers. This is the code
Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
I have a 2-by-3 matrix, and I want to sort it according to the
I have this simple graph: name -> string ^ | v label let matrix
I have a 2D-array that I want to sort based on the second column.
For sorting item names, I want to support numbers correctly. i.e. this: 1 Hamlet
A class project involves sorting an array of strings, with each string containing an
I'm sorting a bunch of IPs, but for some reason they come in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.