Sorting a matrix by the first column Julia - sorting

I have a matrix like this in Julia:
5×2 Array{Float64,2}:
5.52777 7.51186e15
0.00444418 0.0311171
3.26441 9.72657
3.38447 1.7144e16
0.459852 6.90901
I would like to order it by the first column and obtain something like this:
0.00444418 0.0311171
0.459852 6.90901
3.26441 9.72657
3.38447 1.7144e16
5.52777 7.51186e15
How can I do this?

The expected result you posted doesn't show that you want to order it by the first column, so it's not clear what you want.
If you want to sort the matrix by its first column you can use sortperm and indexing:
julia> m[sortperm(m[:,1]),:]
5×2 Array{Float64,2}:
0.00444418 0.0311171
0.459852 6.90901
3.26441 9.72657
3.38447 1.7144e16
5.52777 7.51186e15
sortperm(m[:,1]) returns the sorted indices of the first column which you then use to index the matrix.
A probably faster alternative would be to use sortslices(m,dims=1), but this gets a little bit more difficult to call if e.g. you want to sort by the second column...

Related

Why is sort function not sorting in Google Sheets

I have on row one cell A1 = 3, E1=9, I1=4.
If in cell K1 I write: =CONCATENATE(A1:I1) I get 394
But if I try to sort the cells before concatenating them with =CONCATENATE(SORT(A1:I1)) I still get the same result. Why?
This is due to the principle of the SORT function, which sorts rows according to data in columns. It does not sort individual cells. The example in the link explains more.
If you want to get the result that you anticipate, transpose your data and place them: A1=3, A5=9, A9=4
and perform: =CONCATENATE(SORT(A1:A9, 1, TRUE)), which gives you 349.
Or if you need to stick with row, transpose the cells in between. Use your original cell configuration and perform: =CONCATENATE(SORT(TRANSPOSE(A1:I1), 1, TRUE))
Note: Those additional parameters are required according to the SORT function help, however it seems that 1 and TRUE are used by default if they are missing.

How to get an array of values where columns match multiple criteria

I have a table of data similar to:
where I'd like to get just the shapes which match a set of given criteria (in this case week=2 and colour=blue).
I can return the first result using index and match like:
=ArrayFormula(INDEX(C2:C14,MATCH($F$1&$F$2,A2:A14&B2:B14,0)))
but I'd like to return the all matching values (eg square and triangle) in to the range F3:Fsomething. This would preferably be done using a formula that returns a range and isn't "copied-down", as a list of all possible shapes isn't known beforehand.
How can I modify this formula to achieve this?
See if this works:
=FILTER (C2:C14, B2:B14=F2, A2:A14=F1)
to do multiple criteria you want to use * like so
=FILTER (C2:C14, (B2:B14=F2) * (A2:A14=F1))
and if you want the results all in the same cell with a delimiter, use TEXTJOIN
=TEXTJOIN([DELIMETER],[IGNORE EMPTY TEXT],text1)
=TEXTJOIN(", ",TRUE,FILTER(C2:C14,(B2:B14=F2)*(A2:A14=F1)))

Stack multiple columns into one

I want to do a simple task but somehow I'm unable to do it. Assume that I have one column like:
a
z
e
r
t
How can I create a new column with the same value twice with the following result:
a
a
z
z
e
e
r
r
t
t
I've already tried to double my column and do something like :
=TRANSPOSE(SPLIT(JOIN(";",A:A,B:B),";"))
but it creates:
a
z
e
r
t
a
z
e
r
t
I get inspired by this answer so far.
Try this:
=SORT({A1:A5;A1:A5})
Here we use:
sort
{} to combine data
Accounting your comment, then you may use this formula:
=QUERY(SORT(ArrayFormula({row(A1:A5),A1:A5;row(A1:A5),A1:A5})),"select Col2")
The idea is to use additional column of data with number of row, then sort by row, then query to get only values.
And join→split method will do the same:
=TRANSPOSE(SPLIT(JOIN(",",ARRAYFORMULA(CONCAT(A1:A5&",",A1:A5))),","))
Here we use range only two times, so this is easier to use. Also see Concat + ArrayFormula sample.
Few hundreds rows is nothing :)
I created index from 1 to n, then pasted it twice and sorted by index. But it's obviously fancier to do it with a formula :)
Assuming Your list is in column A and (for now) the times of repeat are in C1 (can be changed to a number in the formula), then something simple like this will do (starting in B1):
=INDEX(A:A,(INT(ROW()-1)/$C$1)+1)
Simply copy down as you need it (will give just 0 after the last item). No sorting. No array. No sheets/excel problems. No heavy calculations.

how can I get the location for the maximum value in fortran?

I have a 250*2001 matrix. I want to find the location for the maximum value for a(:,i) where i takes 5 different values: i = i + 256
a(:,256)
a(:,512)
a(:,768)
a(:,1024)
a(:,1280)
I tried using MAXLOC, but since I'm new to fortran, I couldn't get it right.
Try this
maxloc(a(:,256:1280:256))
but be warned, this call will return a value in the range 1..5 for the second dimension. The call will return the index of the maxloc in the 2001*5 array section that you pass to it. So to get the column index of the location in the original array you'll have to do some multiplication. And note that since the argument in the call to maxloc is a rank-2 array section the call will return a 2-element vector.
Your question is a little unclear: it could be either of two things you want.
One value for the maximum over the entire 250-by-5 subarray;
One value for the maximum in each of the 5 250-by-1 subarrays.
Your comments suggest you want the latter, and there is already an answer for the former.
So, in case it is the latter:
b(1:5) = MAXLOC(a(:,256:1280:256), DIM=1)

jqGrid permutation array

In jqGrid I am trying to use the permutation array for saving the reorder state of the columns.
For eg. Basic column state is perm = [0,1,2,3,4] column 3 is hidden and column 0 is the checkbox. Now I have a custom context menu which I use to finally give me a perm array of [0,1,3,2,4]
I have read in the documentation that the permutation array needs to start with 1, is this right?
When I try using "remapColumns" functions of the jqgrid and pass the perm array, it works fine. But if I try hiding and showing columns a couple of times, the column order is getting messed with.
Please help me understand what these indices for the permutation array stand for? Are they column indexes for visible columns? Should hidden columns be part of array? What happens in case of frozen columns? In some examples I have seen perm = [0:1, 1:3, 2:2, 3:1]
What is the correct way? I am using grid.jqGrid("remapColumns", perm, true);
Try to use also the last parameter of the function
grid.jqGrid("remapColumns", [0,1,3,2,4], true, false);
permutation, updateCells, keepHeader
wiki:methods

Resources