How to Sort by Column [duplicate] - matlab

This question already has answers here:
Sorting entire matrix according to one column in matlab
(2 answers)
Closed 7 years ago.
In Matlab, how would I sort the 6th column where the 6th column represents a top score:
8 5 2 9 5 0
4 6 4 9 3 1
5 2 3 1 8 1
8 3 2 1 6 10
To form an output of:
8 3 2 1 6 10
5 2 3 1 8 1
4 6 4 9 3 1
8 5 2 9 5 0

Assuming your matrix is called A,
B = sortrows(A, -6)
Should do a descending order sort based on the 6th column.
See this matlab documentation for more information.

Related

Take a column of a matrix and make it a row in kdb

Consider the matrix:
1 2 3
4 5 6
7 8 9
I'd like to take the middle column, assign it to a variable, and replace the middle row with it, giving me
1 2 3
2 5 8
7 8 9
I'm extracting the middle column using
a:m[;enlist1]
which returns
2
5
8
How do I replace the middle row with a? Is a flip necessary?
Thanks.
If you want to update the matrix in place you can use
q)show m:(3;3)#1+til 10
1 2 3
4 5 6
7 8 9
q)a:m[;1]
q)m[1]:a
q)show m
1 2 3
2 5 8
7 8 9
q)
cutting out "a" all you need is:
m[1]:m[;1]
You can use dot amend -
q)show m:(3;3)#1+til 10
1 2 3
4 5 6
7 8 9
q)show a:m[;1]
2 5 8
q).[m;(1;::);:;a]
1 2 3
2 5 8
7 8 9
Can see documentation here:
http://code.kx.com/wiki/Reference/DotSymbol
http://code.kx.com/wiki/JB:QforMortals2/functions#Functional_Forms_of_Amend
Making it slightly more generic where you can define the operation, row, and column
q)m:3 cut 1+til 9
1 2 3
4 5 6
7 8 9
Assigning the middle column to middle row :
q){[ m;o;i1;i2] .[m;enlist i1;o; flip[m] i2 ] }[m;:;1;1]
1 2 3
2 5 8
7 8 9
Adding the middle column to middle row by passing o as +
q){[ m;o;i1;i2] .[m;enlist i1;o; flip[m] i2 ] }[m;+;1;1]
1 2 3
6 10 14
7 8 9

Extracting max value from each column in a table

I have generated a table of data with time in one column with attempts 1-10 in the next series of columns. I want to be able to extract the max value in each attempt for further analysis.
I have tried for table MGA
max = max(MGA(:, []))
I get the following error -- "You cannot subscript a table using only one subscript. Table subscripting requires both row and variable subscripts."
First off: Never do max = max();, you'll overload max, and you won't be able to use it again.
And to answer the question, you can do as follows (notice that I've kept the values in the first columns):
MGA
MGA =
1 5 3 8 9
2 4 7 3 3
3 8 7 6 9
4 8 2 7 3
5 2 2 9 10
6 5 5 10 4
7 5 10 6 2
8 7 4 2 3
9 8 6 2 7
10 8 3 3 5
max_values = [MGA(:,1), max(MGA(:,2:end),[],2)]
max_values =
1 9
2 7
3 9
4 8
5 10
6 10
7 10
8 7
9 8
10 8

How to sort rows from smallest to largest using matlab [duplicate]

This question already has answers here:
Matlab: How to sort data for each row/column, loop or formula?
(3 answers)
Closed 8 years ago.
Sorry for so many question i post.
this is my another problem, how to sort rows from smallest to largest, (see picture below):
so here's my data and code:
A=
[a b c
1 2 3
4 5 1
0 1 0
2 1 2]
i used sort function:
B = [sort(A(1:end,:), 'ascend')]
but it didn't display the output i want.
smallest no. display in 1st column and large no. display in last column, so guys please help.
You have two options.
Sortrows: http://www.mathworks.com/help/matlab/ref/sortrows.html
Sort with argument 2, e.g. sort(A, 2):
http://www.mathworks.com/help/matlab/ref/sort.html?refresh=true
I think you're looking for this
a =
1 2 3 4 5
3 6 2 4 7
9 6 5 8 4
B1 = sort(a,2,'ascend')
B1 =
1 2 3 4 5
2 3 4 6 7
4 5 6 8 9
More about this at MathWorks

Matlab: creating a matrix whose rows consist of a linspace or similar pattern

Anybody know a fast way to produce a matrix consisting of a linspace for each row? For example, the sort of pattern I'm looking for in this matrix is:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
...
1 2 3 4 5 6 7 8 9 10
Anyone know any fast tricks to produce this WITHOUT using a for loop?
I just figured this out, so just in case anyone else was troubled by this, we can achieve this exact pattern by:
a=linspace(1,10,10);
b=ones(3,1)*a;
This will give:
>> a = 1 2 3 4 5 6 7 8 9 10
>> b = 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
You need to use repmat.
Example:
>> B = repmat(1:10,[3 1])
B =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
You can vary the value of 3 there. You can change it accordingly.
Another shortcut I can recommend is similar to repmat, but you specify a base array first of a = 1:10;. Once you do this, you specify a series of 1s in the first dimension when indexing which should produce a matrix of the same vectors with many rows as you want, where each row consists of the base array a. As such:
%// number of times to replicate
n = 4;
a = 1:10;
a = a(ones(1,n),:);
Result:
a =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Insert this command: transpose(ndgrid(1:10,1:n));, where n is the number of rows desired in the result.
You can consider these solutions:
With basic matrix indexing (taken from here)
b=a([1:size(a,1)]' * ones(1,NumToReplicate), :) %row-wise replication
b=a(:, ones(NumToReplicate, 1)) %column-wise replication
With bsxfun:
bsxfun(#times,a,(ones(1,NumToReplicate))') %row-wise replication
bsxfun(#times,a',(ones(1,NumToReplicate))) %column-wise replication
You are welcome to benchmark above two solutions with repmat.

Calculate the mean of set of rows in a matrix [duplicate]

This question already has answers here:
Grouping matrix rows in terms of one column
(2 answers)
Closed 9 years ago.
How can I calculate the mean of a group of rows and put the in a matrix such as:
1 4 5
1 34 4
1 65 0
1 5 3
2 3 44
2 52 4
2 5 6
3 9 2
3 9 1
3 9 9
So I can have a matrix as
1 27 4
2 20 18
3 9 4
Thx
Assuming original data is in matrix A:
indx=unique(A(:,1));
for ii=1:numel(indx)
RowMean(ii,:)=mean(A(1,:)==indx(ii),:);
end