1

Is there a way in JavaScript to select a element of a multidimential array. Where the depth/rank/dimensionality is variable and the keys are given by a array of indices. Such that i don't have handle every possible dimentional depth separately. concretely speaking i want do get rid of switch cases like here:

/**
 * set tensor value by index
 * @type {array} indices [ index1, index2, index3 ] -> length == rank.
 * @type {string} value.
 */
tensor.prototype.setValueByIndex = function( indices, value ) {
    var rank = indices.length;

    switch(rank) {
        case 0:
            this.values[0] = value;
        break;
        case 1:
            this.values[indices[0]] = value;
        break;
        case 2:
            this.values[indices[0]][indices[1]] = value;
        break;
        case 3:
            this.values[indices[0]][indices[1]][indices[2]] = value;
        break;
    }
}

Where this.values is a multidimensional array.

such that i get something that looks more like this:

/**
 * set tensor value by index
 * @type {array} indices, [ index1, index2, index3 ] -> length == rank
 * @type {string} value
 */
tensor.prototype.setValueByIndex = function( indices, value ) {
    var rank = indices.length;

    this.values[ indices ] = value;
}

Thank you in advance!

1

4 Answers 4

2
tensor.prototype.setValueByIndex = function( indices, value ) {
    var array = this.values;
    for (var i = 0; i < indices.length - 1; ++i) {
        array = array[indices[i]];
    }
    array[indices[i]] = value;
}

This uses array to point to the nested array we are currently at and reads through the indicies for find the next array value from the current array. Once we reach the last index in the indices list, we have found the array where we want to deposit the value. The final index is the slot in that final array where we deposit the value.

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

Comments

1

Something like this:

tensor.prototype.setValueByIndex = function( indexes, value ) {
    var ref = this.values;  
    if (!indexes.length) indexes = [0];  
    for (var i = 0; i<indexes.length;i++) {
       if (typeof ref[i] === 'undefined') ref[i] = [];
       if (ref[i] instanceof Array) {  
           ref = ref[i];
       } else {
           throw Error('There is already value stored') 
       }
    } 
    ref = value;
}

Comments

1

Why would you want to do that? I'd say writing

tensor.values[1][5][8][2] = value;

is much more perspicuous than

tensor.setValues([1, 5, 8, 2], value);

If you really need to do that, it would be a simple loop over the array:

tensor.prototype.setValueByIndex = function(indices, value) {
    var arr = this.values;
    for (var i=0; i<indices.length-1 && arr; i++)
        arr = arr[indices[i]];
    if (arr)
        arr[indices[i]] = value;
    else
        throw new Error("Tensor.setValueByIndex: Some index pointed to a nonexisting array");
};

2 Comments

Yes it looks trivial but. I want to use a variable "number of indices" to create a abstract tensors such that the rank is equal to the "number of indices". such that all the operations can be defined in a abstract way instead of defining all the operations separately for the different ranks and dimensions
Thanks for the solution. It turns out to be pretty simple :P, Thanks!.
1

Like this?

tensor.prototype.setValueByIndex = function( indices, value ) {
  var t = this, i;
  for (i = 0; i < indices.length - 1; i++) t = t[indices[i]];
  t[indices[i]] = value;
}

1 Comment

No, he wants a set instead of a get

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.