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.

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 28 日
編集済み: Andrei Bobrov 2013 年 10 月 28 日

0 投票

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 件のコメント

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 28 日
編集済み: Andrei Bobrov 2013 年 10 月 28 日
Please see ADD part.

サインインしてコメントする。

Jos (10584)
Jos (10584) 2013 年 10 月 28 日
編集済み: Jos (10584) 2013 年 10 月 28 日

0 投票

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.

カテゴリ

質問済み:

2013 年 10 月 28 日

編集済み:

2013 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by