MATLAB Answers

# How to check a change in one quantity given a known in a separate but related quantity

조회 수: 21(최근 30일)
Tb 3 Mar 2021
Ok, so say you have some data for array A with 100 elements from 0.5e10 to 6e15, for example. Now say you also have data for array B with 100 elements which ranges from -3 to 3. Is it possible to find the change in B for A to increase by a factor of 10 (or an order of magnitude)?
Any help would be greatly appreciated. Thanks.

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

### 답변(1개)

Walter Roberson 4 Mar 2021
format long g
A = randi([0.5e10 6e15],1, 100);
B = rand(1,100)*6-3;
mask = A(2:end)./A(1:end-1) >= 10;
Bdiff = diff(B);
change_leading_to_magnitude = Bdiff(mask);
A(1:10).'
ans = 10×1
1.69972843612337e+15 1.77784990319701e+15 3.00022310672333e+15 4.16154325169978e+15 2.46992279201753e+15 124336416678998 134810902949814 2.40775972431884e+15 575510607379058 5.678509963862e+15
B(1:10).'
ans = 10×1
1.76185437288904 -0.883382907705707 1.50795559189869 0.200808526272912 -0.332818223404338 -0.767763420641153 2.1926931512316 -2.98944392429749 -2.79941171965126 2.09754165823355
find(mask, 5)
ans = 1×3
7 29 92
change_leading_to_magnitude(1:3).'
ans = 3×1
-5.18213707552909 -2.57358770743872 -1.70009597615613
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Walter Roberson 4 Mar 2021
You want to find the places where the value increases by at least a factor of 10 between adjacent items. Assuming the values are all positive, you can do that by taking the ratio of adjacent items, A(2:end)./A(1:end-1), and comparing it to 10. The result, assigned to mask, is a logical vector with one entry for each pair of adjacent positions. mask(K) being true means that A(K+1) is at least 10 times larger than A(K).
You could use find() on the logical vector if you preferred to work with indices, but logical vectors are more efficient.
Your question asked "Is it possible to find the change in B" . I interpreted that to mean that you wanted to know what the difference is between adjacent elements. The difference between adjacent elements is diff(B) and I store that in Bdiff . Then I index it at the places the mask is true.
What I implemented is:
Supposing that A(K+1) is at least 10 times larger than A(K) then calculate B(K+1) - B(K) and put the value into the next available spot in vector change_leading_to_magnitude.

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

### Community Treasure Hunt

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

Start Hunting!