Arranging two arrays in ascending order.

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.

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 28 Okt. 2013
Bearbeitet: Andrei Bobrov am 28 Okt. 2013

0 Stimmen

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);
Jos (10584)
Jos (10584) am 28 Okt. 2013
Bearbeitet: Jos (10584) am 28 Okt. 2013

0 Stimmen

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.

Gefragt:

am 28 Okt. 2013

Bearbeitet:

am 28 Okt. 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by