How to pick values in a matrix due to values in another matrix

조회 수: 3 (최근 30일)
Trond Oesten
Trond Oesten 2015년 3월 3일
댓글: Guillaume 2015년 3월 3일
Hi,
I have two data sets (f and g) with 5 values in each and by using the tiedrank command I have ranked the lowest to the highest value in data set g. What I want to to is pick values in f that correspond to the ranked values in d. So I want to find where the value is 1,2, and so on in d and sample the value at the same index in matrix f. These values will be sampled in f_marked. Is there an easy way to do this in matlab?
clc; clear all; close all;
f = [1 5 9 8 2];
g = [2 4 1 5 7];
d = tiedrank(g);
d = [2 3 1 4 5];
f_marked = [9 1 5 8 2]; % my new sorted vector of f based on d

채택된 답변

Guillaume
Guillaume 2015년 3월 3일
편집: Guillaume 2015년 3월 3일
[~, order] = sort(d);
f_marked = f(order);
Another option:
f_marked = accumarray(d', f)'
  댓글 수: 2
Trond Oesten
Trond Oesten 2015년 3월 3일
This don't work. When I put f_marked = f(d) I get [5 9 1 8 2]. I should get [9 1 5 8 2]...
Guillaume
Guillaume 2015년 3월 3일
Indeed. I wasn't thinking clearly when I wrote my reply.
Edited answer that does give the correct result

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2015년 3월 3일
편집: Stephen23 2015년 3월 3일
The simplest answer is to use d and f directly:
>> x(d) = f
x =
9 1 5 8 2

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by