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
2013년 10월 28일
편집: Andrei Bobrov
2013년 10월 28일
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
2013년 10월 28일
편집: Andrei Bobrov
2013년 10월 28일
Please see ADD part.
Jos (10584)
2013년 10월 28일
편집: Jos (10584)
2013년 10월 28일
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.
카테고리
도움말 센터 및 File Exchange에서 Stability Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!