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

1 次查看(过去 30 天)
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
Walter Roberson 2021-3-4
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 个评论
Tb
Tb 2021-3-4
编辑:Tb 2021-3-4
Thank you for your answer @Walter Roberson, but can you please explain what exactly mask and Bdiff are doing? I don't understand this. Thanks.
Walter Roberson
Walter Roberson 2021-3-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by