How to check a change in one quantity given a known in a separate but related quantity
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
Walter Roberson
el 4 de Mzo. de 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).'
B(1:10).'
find(mask, 5)
change_leading_to_magnitude(1:3).'
2 comentarios
Walter Roberson
el 4 de Mzo. de 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.
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!