Search code examples
arraysmatlablogicnumericnumerical-methods

logic for adjusting max number in array to min number in second array


Greetings all

logic for adjusting max number in array to min number in second array

I have an array "A"

A=[0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1 .9 .8 .7 .6 .5 .4 .3 .2 .1 0 -.1 -.2 -.3 -.4 -.5 -.6 -.7 -.8 -.9 -1 -.9 -.8 -.7 -.6 -.5 -.4 -.3 -.2 -.1]

And I want the second array to be going in the "opposite" direction so when the numbers are going high in array "A" the numbers in array "B" should be going low

example of what array "B" should look like (and A again for reference)

B=[1 .9 .8 .7 .6 .5 .4 .3 .2 .1 0 -.1 -.2 -.3 -.4 -.5 -.6 -.7 -.8 -.9 -1 -.9 -.8 -.7 -.6 -.5 -.4 -.3 -.2 -.1 0 .1 .2 .3 .4 .5 .6 .7 .8 .9]
A=[0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1 .9 .8 .7 .6 .5 .4 .3 .2 .1 0 -.1 -.2 -.3 -.4 -.5 -.6 -.7 -.8 -.9 -1 -.9 -.8 -.7 -.6 -.5 -.4 -.3 -.2 -.1]

I tried using this logic but it makes everything positive of course

arrayB=-abs(arrayA).+abs(max(arrayA));

but that didn't work I'm using matlab but if someone knows the correct logic I can convert it over the matlab syntax

tia

The numbers represent different amplitudes of a signal so when the amplitude of one signal arrayA is going up the other signal arrayB should be going down. There is "overlap"


Solution

  • Your title and description are very confusing, and I'm unable to tell from them what you are trying to do. However, looking at the desired output array B that you want to generate from your input array A, I can give you a couple of operations that will do that transformation for you:

    B = max(A)-cumsum([0 abs(diff(A))].*sign(A+eps));
    

    This finds the absolute differences in values along array A, multiplies these differences by the sign of the elements of A (counting 0 as positive by adding EPS to the signal), takes the cumulative sum of these values, then subtracts them from the maximum value in A.

    Another solution in this case is simply to create a circular shift of A using array indexing. For example, shifting the array A such that its maximum value is at the beginning of the array will give you the desired array B:

    [~,maxIndex] = max(A);
    B = A([maxIndex:end 1:maxIndex-1]);