2

currently I've been trying to shorten numeric arrays without the data values losing relativity to each other.

So first here is an example: Assume an array of length 5

var test = [ 1.0, 0.5, 0.2, 0.5, 1.0 ]; // sum = 3.2

now we want to shorten it to the length of 4 without changing or losing the "curve" it would display as in e.g. a graph.

// wanted output = [ 1.125, 0.475, 0.475, 1.125 ] // sum = 3.2

I've tried to find this under alot of different search terms but couldnt find any example code of what I was trying to do.

2
  • Could you give an explanation of how to map the values into the expected output array? Commented Feb 15, 2019 at 15:49
  • This might be relevant. Commented Feb 15, 2019 at 15:51

1 Answer 1

4

In order to achieve this with variable array lengths and "target" lengths which are not necessarily multiples of each other I came up with this code:

function compressArrayData(arr, toLen) {
    if (!Array.isArray(arr) || isNaN(toLen) || arr.length < toLen) throw "ArgumentError";

    var sizeRatio = arr.length / toLen;

    var results = [];
    var resIndx = 0;
    var rest = 0;
    var lastRatio = sizeRatio;
    var lastVal = 0;
    for (var i = 0; i < arr.length; i++) {
        var r = rest;
        rest = 0;

        var v = arr[i];
        if (lastRatio < 1) {
            var c = v * lastRatio;
            rest = v - c;
            v = c;
        }

        lastRatio -= 1;
        lastVal += v + r;

        if (lastRatio <= 0) {
            results[resIndx++] = lastVal;
            lastVal = 0;
            lastRatio += sizeRatio;
        }
    }
    return results;
}

this creates the exact result as described above.

On how it works:

  • First, we determine how many fields of the origin array are equivalent to one field of the wanted array length (sizeRatio).

  • Then we iterate over the source array and add together the value(s) until the targeted ratio for one target field was reached, then append the value to the results.

  • If the ratio is not %1 = 0, we only add part of the current value and keep the rest to add for the next iteration. => This is the important part for target lengths which are not multiples of each other.

I think the code is pretty good since it doesnt rely on nesting for loops and is done after one iteration of the source array.

However if there should be better ways to do this, preferrably without use of libraries, please share them and let me know what you think.

Sign up to request clarification or add additional context in comments.

6 Comments

This should complete your question, not answer it. Otherwise there is no question.
Well, I am here to share my knowledge and ask for improvements, orr is this not allowed? stackoverflow.blog/2011/07/01/…
@serge wrong! This is an answer. Answers go into the answer section. SO is a collection of a answers, not a Q/A site. Self-answering is a common practice and a good thing
@JonasWilms ok I was a bit confused there, its my first post of this kind, thanks for clearing it up :)
@ThexBasic If you're looking for feedback there is a site dedicated to just do that: codereview.stackexchange.com
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.