JavaScript guide: Converting an array structured by tuples into a multidimensional array

I'm working with an array, X[(i,j,l)], which is indexed by 3-dimensional tuples where i and j range from 1 to n, and l ranges from 1 to "layers". This binary array consists of elements that are either 0 or 1.

The array was generated as a result of solving an optimization problem using opl in CPLEX.

Now, I am attempting to access the values of X as a multidimensional array X[i][j][l] through javascript execution code in the model window.

This is my attempt:

var ofile_varx = new IloOplOutputFile("initial_varx.csv");
ofile_varx.writeln(x);

var x_arr=new Array (n);
for (var i=0; i<n; i++) {
    x_arr[i]=new Array (n);
    for (var j=0; j<n; j++) {
        x_arr[i][j]=new Array (layers);
    }
}           
for (var tup in ijl) {
    x_arr[tup.i][tup.j][tup.l]=x[tup];
}

However, when executing this, I encountered an error on the last line stating that it cannot assign a property "null" to the array.

Do you have any suggestions on how I can achieve the desired array x_arr successfully?

Thank you!

Answer №1

struct t
{
 int x;
 int y;
 int z;
}

int num=4;
int levels=5;
{t} xyz={<1,2,3>,<1,2,1>}

dvar int nums[xyz];
subject to
{
forall(num in xyz) nums[num]==num.x;
}


execute
{
var nums_arr=new Array (num);
for (var i=0; i<=num; i++) {
    nums_arr[i]=new Array (num);
    for (var j=0; j<=num; j++) {
        nums_arr[i][j]=new Array (levels);
    }
}           
for (var tupl in xyz) {
    nums_arr[tupl.x][tupl.y][tupl.z]=nums[tupl].solutionValue;
}
}

functions properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modify the structure of the JSON string

My JSON string is structured like this: [ { "queryResult": { "A": "12-04-2014", "B": 1 } }, { "queryResult": { "A": "13-04-2014", "B": 2 } }, { "qu ...

What is the reason behind having all bindings resolved during each $digest cycle?

Upon placing a breakpoint on a bound function, I discovered that it is being triggered every cycle. This came as a surprise to me as the timer displaying a countdown on the page was updating other properties as well. The demonstration below showcases thre ...

Creating functionality with a native JavaScript plugin within a directive and test suite

I have a custom JavaScript plugin that is integrated within a directive and utilized in an Angular manner. Snippet of the directive, export default function () { 'use strict'; return { restrict: 'E', scope: { map: &apo ...

Troubleshooting event binding problems with jQuery

<div id="parent"> <div id="children"> </div> </div> If we attach the same events to both parent and children elements: $("#parent").live({ mouseenter : Infocus , mouseleave : Outfocus }); $("#childre ...

How can I pass a DOM element as a prop in Vue 3?

As someone who is brand new to Vue, I'm exploring the possibilities. Although it's not something I typically do, I believe achieving a similar outcome in React + JSX (untested) could look like this: render() { const el = <p>Blah <a hre ...

Remove the redundant rows and columns in the matrix

I am trying to implement a program that deletes rows and columns of a matrix that appear more than once, without skipping the duplicates. The goal is to only print the first row from the top that appears more than once or the first column from the left tha ...

Transform the string by eliminating any spaces and new lines before converting it into a JSON object

I need assistance with converting the given string into a JSON object. Here is the string: {\"Warranty\": [ \n { \n \"Name\": \"test\", \n \"Type\": \"test2\", \n \"Months\": ...

Unable to retrieve information from v-for as it returns null data

Currently facing an issue with retrieving data from the database using Axios in Vue.js. I am able to see the data in my database through Vue.js developer tools like this: https://i.stack.imgur.com/n7BRO.png However, when attempting to loop through the dat ...

Creating a customized file input using CSS

I am currently utilizing Bootstrap4 to craft my webpage and I am incorporating a custom file input bar. To retrieve the three labels of the input bar ("Choose file", "Browse", "Upload") from my stylesheet, I have created three custom classes with their co ...

use JavaScript to automatically select checkboxes based on their value

I've been facing a challenge for the past week with one particular issue. I have two arrays and I'm trying to automatically select checkboxes based on the values stored in one of the arrays. Initially, I use a loop to generate checkboxes based o ...

Differences between Using Generic Array Creation and Array.newInstance()

I am in the process of creating a class that consists of two ArrayList fields. The first field is for storing custom objects, while the second field is for storing collections of custom objects (which may also have these fields): private ArrayList<Some ...

Checking authentication globally using Vue.js

In the main blade file, I have the following code snippet: <script> window.App = {!! json_encode([ 'csrfToken' => csrf_token(), 'user' => Auth::user(), 'signedIn' => Auth::check() ...

Creating an error handler in PHP for dynamically adding or removing input fields is a crucial step in ensuring smooth and

I designed a form with multiple fields, including a basic input text field and dynamic add/remove input fields. I successfully set the first field as required using PHP arguments, but I'm struggling to do the same for the additional fields. I need as ...

Exploring methods to trace the factory's property that is receiving updates from another factory within AngularJS

Hey there, I'm new to Angularjs and I have a bunch of factories in my application. The situation is, let's say we have obj1 in factoryA. Whenever I console.log(obj1), it displays numerous properties associated with it. This object is being update ...

expanding the values associated with a key in python

Looking to make a change, adding the following: { "Category": "Fruit" } into this block: { "Category": "Vegetable" { resulting in: "Name": "Menu1", "Categories":[ { &q ...

Eliminate the presence of Null within an ArrayList's For loop

I am currently in the process of converting an ArrayList into a String for the purpose of sending it to a database. The intention is to retrieve it on the receiving end and later convert it back to an ArrayList. My current approach involves converting the ...

How can you make an Angular directive activate only after the scope function in an ng-click handler has been executed?

Scenario I am relatively new to Angular and facing a specific challenge. The goal is to make a directive change the color and add an image to a button. However, I am struggling to get the first if condition to work in my Angular Directive. Issue The ob ...

Problem with Resetting State in Cordova/AngularJS

Currently, I am facing a challenge with my Cordova/AngularJS mobile application as I struggle to refresh the current state with new data. Please be aware that this is not an Ionic Framework application. The issue arises in the part of my app where I need ...

Vue Router displays a blank page after being compiled

Seeking assistance here. I have built my application using vuejs and everything seems to be working fine. However, when I run npm run build, extract the dist folder, and open index.html, all I see is a blank page with no errors in the console. main.js impo ...

Tips for adding and deleting elements after cloning in jQuery

I'm trying to achieve a layout similar to the one shown in this picture. It should display a delete button for each item and an add button to add more items. Check out the example here. I have managed to display the buttons individually, but I'm ...