matlab sort one column and keep respective values on second column - matlab

How do I just do a simple sort in matlab. I always have to use the excel link to import my data, sort it, then export back to matlab. This is annoying!!!
I have one matrix <10x10> and I want to sort the first column in descending order while keeping it's respective values on the second column. Matlab seems to just sort each column individually.
Example:
matrix a
5 4
8 9
0 6
7 3
matrix b (output)
0 6
5 4
7 3
8 9

The sortrows answer by #chaohuang is probably what you're looking for. However, it sorts based on all columns. If you only want to sort based on the first column, then you can do this:
% sort only the first column, return indices of the sort
[~,sorted_inds] = sort( a(:,1) );
% reorder the rows based on the sorted indices
b = a(sorted_inds,:);

Simply use b=sortrows(a); See here.

Related

Sort rows of a matrix in ascending order

I would like to sort a matrix in ascending order, however I do not want to affect the third column. For example, the sorted version of
A= [ 2 1 3;
5 4 1;
4 3 2]
Would be
B= [1 2 3;
4 5 1;
3 4 2]
Matlab provides quite a bit of inhouse help so using help FUNCTION/CLASS would have provided you with the below information. If you don't know the FUNCTION\CLASS name use lookfor TERM for a list of matches or alternately docsearch TERM.
Stock matlab provides both sort and sortrows. You'll be needing the latter.
sortrows(X,C)
Where C is a list of column indices to sort by whose sign may be positive corresponding for ascending order or negative for descending order.
In your example you'll want this :
sortrows(A',[1,2])'
The ' indicates to matlab that you need the matrix transposed, which basically swaps rows and columns before and after sortrows is called.
You can just sort the 1st two columns and update the matrix accordingly:
edit: updated dimension
A(:,1:2) = sort(A(:,1:2),2);

Is MATLAB row specific or column major?

In MATLAB, we can operate on both rows and columns of a matrix. What does it exactly mean by "row major" or "column major"?
It is important to understand that MATLAB stores data in column-major order, so you know what happens when you apply the colon operator without any commas:
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> M(:)
ans =
8
3
4
1
5
9
6
7
2
I tend to think "MATLAB goes down, then across". This makes it easy to reshape and permute arrays without scrambling your data. It's also necessary in order to grasp linear indexing (e.g. M(4)).
For example, a common way to obtain a column vector inline from some expression that generates an array is:
reshape(<array expression>,[],1)
As with (:) this stacks all the columns on top of each other into single column vector, for all data in any higher dimensions.
But this nifty syntactic trick lets you avoid an extra line of code.
In MATLAB, arrays are stored in column major order.
It means that when you have a multi-dimensional array, its 1D representation in memory is such that leftmost indices change faster.
It's called column major order because for a 2D array (matrix), the first (leftmost) index is typically the row index, so since it changes faster than the second (next to the right) index, the 1D representation of the matrix is memory correspond to the concatenation of the columns of the matrix.

MATLAB: copy a specific portion of an array

I am trying to copy a few elements from a matrix, but not a whole row, and not a single element.
For example, in the following matrix:
a = 1 2
3 4
5 6
7 8
9 0
How would I copy out just the following data?
b = 1
3
5
i.e. rows 1:3 in column 1 only... I know that you can remove an entire column like this:
b = a(:,1)
... and I appreciate that could just do this and then dump the last two rows, but I'd like to use more streamlined code as I am running a very resource-intensive solution.
Elements in a matrix in MATLAB are stored in column-major order. Which means, you could even use a single index and say:
b = a(1:3);
Since the first 3 elements ARE 1,3,5. Similarly, a(6) is 2, a(7) is 4 etc. Look at the sub2ind method to understand more:
http://www.mathworks.com/help/techdoc/ref/sub2ind.html
You are not "removing" the second column, you are referencing the other column.
You should read some of the Matlab docs, they provide some help about the syntax for accessing portions of matrices:
http://www.mathworks.com/help/techdoc/learn_matlab/f2-12841.html#f2-428

Removing rows with identical first column value in matlab

I have a cell matrix of size 10000 X 3 in Matlab and I would like to remove rows with the same value in the first column.
That is, if row i and row j have the same value in the first column, I'd like to delete both rows.
I should also say that there can be more than two rows with the same value in the first column and in that case, I'd like to delete all these rows.
How do I do it?
Thanks!
You can use the functions histc, unique and logical indexing to achieve what you want. Here's a small example.
a=randi(10,5,3) %#generate a sample random matrix
a =
5 3 5
5 7 10
7 7 4
8 2 6
8 2 3
[uniqVals,uniqIndx]=unique(a(:,1)); %# get unique values and corresponding indices of the first column of a
count=histc(a(:,1),uniqVals); %# get the bin counts of the elements (i.e., find which are repeated)
b=a(uniqIndx(count==1),:)
b =
7 7 4
Only the row with the non-repeated element is selected. Since you said that you have a cell matrix, simply covert it to a matrix using cell2mat before doing this.

How can I sort a 2-D array in MATLAB with respect to one column?

I would like to sort a matrix according to a particular column. There is a sort function, but it sorts all columns independently.
For example, if my matrix data is:
1 3
5 7
-1 4
Then the desired output (sorting by the first column) would be:
-1 4
1 3
5 7
But the output of sort(data) is:
-1 3
1 4
5 7
How can I sort this matrix by the first column?
I think the sortrows function is what you're looking for.
>> sortrows(data,1)
ans =
-1 4
1 3
5 7
An alternative to sortrows(), which can be applied to broader scenarios.
save the sorting indices of the row/column you want to order by:
[~,idx]=sort(data(:,1));
reorder all the rows/columns according to the previous sorted indices
data=data(idx,:)