Turn a vector's indexes into its content and vice-versa

mjgalindo

I'm looking an efficient way to turn the vector:

[1,1,1,2,3,3,3,4,4,4,5,1]

into a vector of vectors such that:

[[1,2,3,12],[4],[5,6,7],[8,9,10],[11]]

In general:

newVector[i] = indexes of the initial vector that contained i

Preferably in Matlab/Octave but I'm just curious if there is an efficient way of achieving this.

I tried looking it up on google and stack but I have no idea what to call this 'operation' so nothing came up.

Dan

There is an easy way to do it using accumarray

A = [1,1,1,2,3,3,3,4,4,4,5,1]
accumarray(A',A',[],@(x){find(ismember(A,x))})

But next time, please show your own attempt in your question

Alternatively (but only if A starts from 1 and doesn't skip any numbers)

accumarray(A', (1:size(A,2))', [], @(x){sort(x)})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Turn a vector's indexes into its content and vice-versa

From Dev

Change td content to select and vice versa

From Dev

Convert a symbol to its 4 digit unicode escape representation and vice versa

From Dev

PDF file content to Base 64 and vice versa in Java

From Dev

Aggregate vector by its content

From Dev

rails assign variable only when it's ajax request or vice versa

From Dev

How to recognize the movement of the UISlider is from maximum to its minimum value and its Vice Versa?

From Dev

Replacing indexes on each element and its content

From Dev

XML To JSON and Vice Versa

From Dev

What is the way to know a Zope permission title by knowing its id (and vice-versa)?

From Dev

How to copy from an array to a Vector256 and vice versa based on the array index?

From Dev

Either to Try and vice versa in Scala

From Dev

DeflateStream / GZipStream to CryptoStream and vice versa

From Dev

dateTime to Epoch and vice versa xslt

From Dev

Convert Intent to String and vice versa

From Dev

Converting int[] to short[] and vice versa

From Dev

Object to string, and vice-versa

From Dev

Assign ViewController to Class or vice versa

From Dev

hadoop mapreduce vice-versa

From Dev

BitmapImage to byte array and vice versa

From Dev

Either to Try and vice versa in Scala

From Dev

Change selectbox to textfield and vice versa

From Dev

Java String to Json & vice versa

From Dev

Java Datatypes to Swift and vice versa

From Dev

Server interact with client and vice versa

From Dev

Compress and then encrypt, or vice-versa?

From Dev

Assign ViewController to Class or vice versa

From Dev

wxSlider updating wxTextCtrl and Vice Versa

From Dev

Regress y on x and vice versa

Related Related

HotTag

Archive