# Matrix Column/Row Labelling

Hi,

Is there a simple way of assigning labels to matrix columns such that if operations are performed to that matrix you still can see where that specific column has ended up for example reduced row echelon or if I performed a ordering algorithm etc.

Given a 3x3 that has the form below

`````` 1  2  3
4  5  6
7  8  9
``````

I want to assign labels such that col1=a, col2 = b and col3=c.

So if I called `a` it would return the result

``````1
4
7
``````

Then if I wrote an algorithm such that it returned which columns are the largest subject to their column 1-norm in this case I would want to the algorithm to return ` c b a`.

Just to clarify not after an algorithm just the column labeling part.

Hopefully that examples clears up what I am looking for Cheers,
Harry

Array indices can only be integers. However, you can attach labels/keys using a keyed array package:

``````julia> using AxisKeys

julia> A = KeyedArray([1 2; 3 4], x=[:a, :b], y=[:c, :d])
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   x ∈ 2-element Vector{Symbol}
→   y ∈ 2-element Vector{Symbol}
And data, 2×2 Matrix{Int64}:
(:c)  (:d)
(:a)   1     2
(:b)   3     4

# regular index of the largest column
julia> argmax(sum(abs, c) for c in eachcol(A))
2

# label of the largest column:
julia> axiskeys(A, :y)[argmax(sum(abs, c) for c in eachcol(A))]
:d
``````
2 Likes

Using DataFrames for column labelling:

``````using DataFrames
m = [1  2  3; 4  5  6; 7  8  9]
df = DataFrame(m,  [:a, :b, :c])
df.a
names(df, sortperm(map(c->sum(abs, c), eachcol(df)), rev=true))
``````

Or using DataFrames and named tuples:

``````nrm1 = map(c->sum(abs, c), Tables.columntable(df))
using TupleTools  # to be able to sort tuples
getproperty.((keys(nrm1),), TupleTools.sortperm(values(nrm1), rev=true))
``````

You have two ways. Either use the DataFrame package. That’s a mature and large package that, aside providing a named cols api, allow to efficiently work on heterogeneous yabular data (e.g. one column is an integer, another a string,…).
While dataframes do support the so called “Table” API, they sre not AbstractArrays.
If you want a ligher package that desls only with labeled indexes keeping an AbstractArray behaviour, there sre tens of pachages, like the mentioned KeiedArrays pachage… there sre several threads on this in the forun…