필터 지우기
필터 지우기

how to find where a value falls in a list

조회 수: 10 (최근 30일)
steve
steve 2023년 12월 3일
편집: Voss 2023년 12월 4일
I have a 2 x 1000 array (“A”) as shown in the first 2 columns. (I only include the first 10 rows). The values are in order.
Then I have another array(“B”) as shown in the third column, which is also in order. I want to find a set of weights that identify the weights of corresponding values in the first two columns.
For example, .029 is in between .000 and .053 with respective weights 0.72 and 0.28. Also, .117 is between .105 and .157
How do i calculate the 4 columns of “C” shown below. If I know C(1.x), i can do the rest, but need to identify the Index immediately before each of the values in B.
chart appears below
  댓글 수: 4
Taylor
Taylor 2023년 12월 4일
You could just use the find function. It would look something like this
idx = find(A(:,2) < B(1), 1)
In this case, the find function will return the index where the first value of the second column of A that is smaller than the first value of B occurs.
steve
steve 2023년 12월 4일
thank you

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

채택된 답변

Voss
Voss 2023년 12월 4일
A = [
1 0
2 0.053
3 0.105
4 0.157
5 0.208
6 0.258
7 0.306
8 0.353
9 0.399
10 0.442
];
B = [
0.015
0.029
0.044
0.059
0.073
0.088
0.102
0.117
0.132
];
One way to determine the index in A(:,2) of the first element greater than each element of B:
N = numel(B);
idx = zeros(N,1);
for ii = 1:N
idx(ii) = find(A(:,2) > B(ii), 1);
end
Another way to do the same:
[~,idx] = max(A(:,2) > B(:).', [], 1);
Then the matrix C is:
C = [idx(:)-[1 0] [A(idx,2)-B B-A(idx-1,2)]./(A(idx,2)-A(idx-1,2))]
C = 9×4
1.0000 2.0000 0.7170 0.2830 1.0000 2.0000 0.4528 0.5472 1.0000 2.0000 0.1698 0.8302 2.0000 3.0000 0.8846 0.1154 2.0000 3.0000 0.6154 0.3846 2.0000 3.0000 0.3269 0.6731 2.0000 3.0000 0.0577 0.9423 3.0000 4.0000 0.7692 0.2308 3.0000 4.0000 0.4808 0.5192
Of course, if all you want to do with all this is to interpolate something, then you can let interp1 do the calculations of the weights and whatnot for you.
  댓글 수: 2
steve
steve 2023년 12월 4일
thank you
Voss
Voss 2023년 12월 4일
편집: Voss 2023년 12월 4일
You're welcome! Any questions, let me know.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by