Compare 2 time series, array and map to third.

Hi,
I have a random column vector A sorted from high to low. I have to map the position of its each entry on column vector B (again sorted high to low), and use that indexing to write a new function C.
Here's an example:
B = [3, 2.5, 2.0, 1.5, 1.0]';
%% this BB column vector represents values from intervals: 0 to 1.0, 1.0 to 1.5, 1.5 to 2.0, 2.0 to 2.5, 2.5 to 3.0
A = [3.3, 2.4, 2.2, 1.3, 0.5]'
idx_A = zeros(5,1); % ??? finds the position of A on the point intervals of B.
% I'm lookimg to use a formula or a faster way to get the following result.
idx_A(1,1) = 1; % as A(1,1) is greater than the 1st value of B.
idx_A(2,1) = 3; % as A(2,1) is greater than the 3rd value of B but smaller than 2nd value of B
idx_A(3,1) = 3; % as A(3,1) is greater than the 3rd value of B but smaller than 2nd value of B
idx_A(4,1) = 5; % as A(4,1) is greater than the 5th value of B but smaller than 4nd value of B
idx_A(5,1) = 5; % A(5,1) is lower than the least value of B so should be equal to the lowest value of B, which is 5.
% after finding idx_A, there is another column vector D which is used to find C as:
C = D(idx_A,1);
display(D)
many thanks for your help.

 채택된 답변

Adam Danz
Adam Danz 2019년 5월 28일
편집: Adam Danz 2019년 5월 28일

0 개 추천

This should work for you. The last value of B is changed to -inf so that all values other than NaN will be greater than or equal to the last one.
B(end) = -inf;
idx_A = arrayfun(@(x)find(x>=B,1),A);
Result
idx_A =
1
3
3
5
5
*Assumes B is sorted in descending order as described by OP.

댓글 수: 2

J Yadav
J Yadav 2019년 5월 28일
Thank you very much.
Adam Danz
Adam Danz 2019년 5월 28일
Glad I could help!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2019년 5월 28일

편집:

2019년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by