Arranging two arrays in ascending order.
Show older comments
I have two two-dimensional arrays, we'll say A and B. "A" is an array of temperature values, and "B" is an array of thermal diffusivity values. The indices of A and B are linked, such that the diffusivity at temperature A(i,j,k) is B(i,j,k), respectively. Or, the temperature at A(351,53) yields the diffusivity B(351,53).
I need to plot this relation, diffusivity as a function of temperature. However the arrays are not in ascending order, but more random.
Therefore, I need to rearrange the temperature (A) array in ascending order while rearranging the diffusivity (B) array at the same time. However due to the way the initial data is measured, it is not necessarily true that using the function "sortrows" on both arrays will have each B(i,j) line up with the original A(i,j).
So essentially I want to do sortrows(A), and have each element B(i,j) move when A(i,j) moves.
This seems kind of hard to explain, so I hope this makes since.
Answers (2)
Andrei Bobrov
on 28 Oct 2013
Edited: Andrei Bobrov
on 28 Oct 2013
data = unique([A(:),B(:)],'rows');
ADD
A = randi(20,8);
B = randi([100 120],8);
data = unique([A(:),B(:)],'rows');
[out,c,c] = unique(data(:,1))
out(:,2) = accumarray(c,data(:,2),[],@mean);
1 Comment
Andrei Bobrov
on 28 Oct 2013
Edited: Andrei Bobrov
on 28 Oct 2013
Please see ADD part.
Jos (10584)
on 28 Oct 2013
Edited: Jos (10584)
on 28 Oct 2013
Use the second output of sort:
A = magic(3) % unsorted values
B = reshape(1:numel(A),size(A))
[sortedA,ix] = sort(A(:)) % sort A
sortedB = B(ix) % and reshuffle B accordingly
% reshape if you really need to
sortedA = reshape(sortedA, size(A))
sortedB = reshape(sortedB, size(B))
And if you insist on using sorrows
sortedAB = sortrows([A(:) B(:)])
sortedA = sortedAB(:,1)
% etc.
Categories
Find more on Stability Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!