Sorting data in MATLAB dependant on one column - matlab

How do I sort a column based on the values in another column in MATLAB?
Column A shows position data (it is neither ascending or descending in order) Column B contains another column of position data. Finally column C contains numerical values. Is it possible to link the first position value in B with its numerical value in the first cell of C? Then after this I want to sort B such that it is in the same order as column A with the C values following their B counterparts?The length of my columns would be 1558 values.
Before case;
A B C
1 4 10
4 1 20
3 5 30
5 2 40
2 3 50
After Case;
A B C
1 1 20
4 4 10
3 3 50
5 5 30
2 2 40
Basically A and B became the same and Column C followed B.

Since you don't want things necessarily in ascending or descending order, I don't think any built-in sorting functions like sortrows() will help here. Instead you are matching elements in one column with elements in another column.
Using [~,idx]=ismember(A,B) will tell you where each element of B is in A. You can use that to sort the desired columns.
M=[1 4 10
4 1 20
3 5 30
5 2 40
2 3 50];
A=M(:,1); B=M(:,2); C=M(:,3);
[~,idx]=ismember(A,B);
sorted_matrix = [A B(idx) C(idx)]

Powerful combo of bsxfun and matrix-multiplication solves it and good for code-golfing too! Here's the implementation, assuming M as the input matrix -
[M(:,1) bsxfun(#eq,M(:,1),M(:,2).')*M(:,2:3)]
Sample run -
>> M
M =
1 4 10
4 1 20
3 5 30
5 2 40
2 3 50
>> [M(:,1) bsxfun(#eq,M(:,1),M(:,2).')*M(:,2:3)]
ans =
1 1 20
4 4 10
3 3 50
5 5 30
2 2 40

Given M = [A B C]:
M =
1 4 10
4 1 20
3 5 30
5 2 40
2 3 50
You need to sort the rows of the matrix excluding the first column:
s = sortrows(M(:,2:3));
s =
1 20
2 40
3 50
4 10
5 30
Then use the first column as the indices to reorder the resulting submatrix:
s(M(:,1),:);
ans =
1 20
4 10
3 50
5 30
2 40
This would be used to build the output matrix:
N = [M(:,1) s(M(:,1),:)];
N =
1 1 20
4 4 10
3 3 50
5 5 30
2 2 40
The previous technique will obviously only work if A and B are permutations of the values (1..m). If this is not the case, then we need to find the ranking of each value in the array. Let's start with new values for our arrays:
A B C
1 5 60
6 1 80
9 6 60
-4 9 40
5 -4 30
We construct s as before:
s = sortrows([B C]);
s =
-4 30
1 80
5 60
6 60
9 40
We can generate the rankings one of two ways. If the elements of A (and B) are unique, we can use the third output of unique as in this answer:
[~, ~, r] = unique(A);
r =
2
4
5
1
3
If the values of A are not unique, we can use the second return value of sort, the indices in the original array of the elements in sorted order, to generate the rank of each element:
[~, r] = sort(A);
r =
4
1
5
2
3
[~, r] = sort(r);
r =
2
4
5
1
3
As you can see, the resulting r is the same, it just takes 2 calls to sort rather than 1 to unique. We then use r as the list of indices for s above:
M = [A s(r, :)];
M =
1 1 80
6 6 60
9 9 40
-4 -4 30
5 5 60

If you must retain the order of A then use something like this
matrix = [1 4 10; 4 1 20; 3 5 30; 5 2 40; 2 3 50];
idx = arrayfun(#(x) find(matrix(:,2) == x), matrix(:,1));
sorted = [matrix(:,1), matrix(idx,2:3)];

Related

Summing specific columns for each row in a matrix of double

I would like to sum specific columns of each row in a matrix using a for loop. Below I have included a simplified version of my problem. As of right now, I am calculating the column sums individually, but this is not effective as my actual problem has multiple matrices (data sets).
a = [1 2 3 4 5 6; 4 5 6 7 8 9];
b = [2 2 3 4 4 6; 3 3 3 4 5 5];
% Repeat the 3 lines of code below for row 2 of matrix a
% Repeat the entire process for matrix b
c = sum(a(1,1:3)); % Sum columns 1:3 of row 1
d = sum(a(1,4:6)); % Sum columns 4:6 of row 1
e = sum(a(1,:)); % Sum all columns of row 1
I would like to know how to create a for loop that automatically loops through and sums the specific columns of each row for each matrix that I have.
Thank you.
Here is a solution that you don't need to use for loop.
Assuming that you have a matrix a of size 2x12, and you want to do the row sums every 4 columns, then you can use reshape() and squeeze() to get the final result:
k = 4;
a = [1:12
13:24];
% a =
% 1 2 3 4 5 6 7 8 9 10 11 12
% 13 14 15 16 17 18 19 20 21 22 23 24
s = squeeze(sum(reshape(a,size(a,1),k,[]),2));
and you will get
s =
10 26 42
58 74 90

sum of values in a column corresponding to first and last occurrence of a number in another column - matlab

a = [1 1 1 1 2 2 2 2 3 3 3 3; 1 2 3 4 5 6 7 8 9 10 11 12]';
What is the quickest way to sum the values in column 2 that correspond to the first and last occurrence of each number in column 1?
The desired output:
1 5
2 13
3 21
EDIT: The result should be the same if the numbers in column 1 are ordered differently.
a = [2 2 2 2 1 1 1 1 3 3 3 3; 1 2 3 4 5 6 7 8 9 10 11 12]';
2 5
1 13
3 21
You can use accumarray as follows. Not sure how fast it is, especially because it uses a custom anonymous function:
[u, ~, v] = unique(a(:,1), 'stable');
s = accumarray(v, a(:,2), [], #(x) x(1)+x(end));
result = [u s];
If the values in the first column of a are always in contiguous groups, the following approach can be used as well:
ind_diff = find(diff(a(:,1))~=0);
ind_first = [1; ind_diff+1];
ind_last = [ind_diff; size(a,1)];
s = a(ind_first,2) + a(ind_last,2);
result = [unique(a(:,1), 'stable') s];

matlab shuffle elements of vector with the same sequent of the same number

I have the following vector
a = 3 3 5 5 20 20 20 4 4 4 2 2 2 10 10 10 6 6 1 1 1
does anyone know how to shuffle this vector with the same elementsnever be seperate?
something like bellow
a = 10 10 10 5 5 4 4 4 20 20 20 1 1 1 3 3 2 2 2 6 6
thank you, best regard...
You can use unique combined with accumarray to create a cell array where each group of values is placed into a separate cell element. You can then shuffle these elements and recombine them into an array.
% Put each group into a separate cell of a cell array
[~, ~, ind] = unique(a);
C = accumarray(ind(:), a(:), [], #(x){x});
% Shuffle it
shuffled = C(randperm(numel(C)));
% Now make it back into a vector
out = cat(1, shuffled{:}).';
% 20 20 20 1 1 1 3 3 10 10 10 5 5 4 4 4 6 6 2 2 2
Another option is to get the values using unique and then compute the number that each occurs. You can then shuffle the values and use repelem to expand out the result
u = unique(a);
counts = histc(a, u);
% Shuffle the values
inds = randperm(numel(u));
% Now expand out the array
out = repelem(u(inds), counts(inds));
A very similar answer to #Suever, using a loop and logical matrix rather than cells
a = [3 3 5 5 20 20 20 4 4 4 2 2 2 10 10 10 6 6 1 1 1];
vals = unique(a); %find unique values
vals = vals(randperm(length(vals))); %shuffle vals matrix
aout = []; %initialize output matrix
for ii = 1:length(vals)
aout = [aout a(a==(vals(ii)))]; %add correct number of each value
end
Here's another approach:
a = [3 3 5 5 20 20 20 4 4 4 2 2 2 10 10 10 6 6 1 1 1];
[~, ~, lab] = unique(a);
r = randperm(max(lab));
[~, ind] = sort(r(lab));
result = a(ind);
Example result:
result =
2 2 2 3 3 5 5 20 20 20 4 4 4 10 10 10 1 1 1 6 6
It works as follows:
Assign unique labels to each element of a depending on their values (this is vector lab);
Apply a random bijection from the values of lab to themselves (the random bijection is represented by r; the result of applying it is r(lab));
Sort r(lab) and get the indices of the sorting (this is ind);
Apply those indices to a.

MATLAB: how to replace values by another values for each row WITHOUT a loop

I have a matrix A made of 1 2 and 3, e.g.,
A= [ 1 2 2 1;
3 3 1 2;
...
...
1 1 2 2]
now I want to replace 1 2 3 with different values in B according to its row. e.g.,
B= [ 4 5 6;
10 20 30;
...
...
77 88 99]
I want to replace the value in A to B in each row. e.g.,
A= [ 1 2 2 1; replace '1 2 3' with '4 5 6' respectively
3 3 1 2; replace '1 2 3' with '10 20 30' respectively
...
...
1 1 2 2] replace '1 2 3' with '77 88 99' respectively
C will be the matrix with the new assignments that I want.
C= [ 4 5 5 4;
30 30 10 20;
...
...
77 77 88 88]
I can't avoid a loop in this case, here is the quickest way that I can do:
for row_i=1:size(A,1)
C(row_i,:)=B(row_i,A(row_i,:))
end
I hate loops in MATLAB, and the actual size of A and B are large, therefore wonder if anyone can reproduce it without loop will be highly appreciated!
A = [ 1 2 2 1;
3 3 1 2;
1 1 2 2];
B = [ 4 5 6;
10 20 30;
77 88 99];
C = B(sub2ind(size(col), repmat(1:size(A, 1), size(A, 2), 1).', A));
4 5 5 4
30 30 10 20
77 77 88 88
Explanation
You're using A to indicate which column of B to use. And as you said, you want the row to be the same as the row in A. So we just need a row and column index into B for each member of A.
To get the rows, we just need to create a matrix the size of A where every element in each row is equal to the row index. Also we take care to avoid any magic numbers and actually determine the proper size.
rowindex = repmat(1:size(A, 1), size(A, 2), 1).'
1 1 1 1
2 2 2 2
3 3 3 3
Great, now that we have that, we already know the columns. That is simply A!
colindex = A
1 2 2 1
3 3 1 2
1 1 2 2
Now we just need to convert these subscripts to absolute indices using sub2ind and the size of B as a reference.
indices = sub2ind(size(B), rowindex, colindex)
1 4 4 1
8 8 2 5
3 3 6 6
Now we just need to use these to index into B and assign to C.
C = B(indices)
4 5 5 4
30 30 10 20
77 77 88 88

Extracting unique values

I have data in two columns that looks as follows:
A B
1,265848208 3
-0,608043611 0
-0,285735893 0
0,006895134 7
0 7
-0,004526196 7
0,176326617 10
-0,159688071 2
0,22439945 2
-0,991045044 1
0,178022324 1
-0,270967397 4
0,285849994 4
1,881705539 23
1,057184204 10
NaN 10
For all unique values in B I want to extract the corresponding value in column A and move it to a new matrix. I'm looking to then compute the mean of all the corresponding values in A and use as a dependent variable (weighted by no of observations per value in B) in a regression with the common value of B being the independent variable to reduce noise. Any help would on how to do this in Matlab (except running the regression) would be great!
Thanks
Oscar
Here is an efficient solution:
X = [
1.265848208 3
-0.608043611 0
-0.285735893 0
0.006895134 7
0 7
-0.004526196 7
0.176326617 10
-0.159688071 2
0.22439945 2
-0.991045044 1
0.178022324 1
-0.270967397 4
0.285849994 4
1.881705539 23
1.057184204 10
NaN 10
];
%# unique values in B, and their indices
[valB,~,subs] = unique(X(:,2));
%# values of A for each unique number in B (cellarray)
valA = accumarray(subs, X(:,1), [], #(x) {x});
%# mean of each group
meanValA = cellfun(#nanmean, valA)
%# perform regression here...
The result:
%# B values, mean of corresponding values in A, number of A values
>> [valB meanValA cellfun(#numel,valA)]
ans =
0 -0.44689 2
1 -0.40651 2
2 0.032356 2
3 1.2658 1
4 0.0074413 2
7 0.00078965 3
10 0.61676 3
23 1.8817 1