Message Boards Message Boards

0
|
2324 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Convert Dataset into array

Posted 2 years ago

Hello, I am trying to perform the following code on a dataset:

Dataset[1]=-1;
    For [i=2,i<7000,i++,

    Dataset[i]=Dataset[i-1]+Dataset[i+1]

    ]

The only way i can see to make this happen is to convert the dataset into an array and performing the loop. Here is a sample of the Dataset: {-1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, -1, \ 0, 0, 1, -1, 1, 0, -1, 1, 1, -1, 1, 0, 0, 0, 0, 0, -1, 0, 0, -1, 1, 1, 1,....} Here is the result i wanna achieve: {-1, -1, 0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6, 7, 8, 9, 8, 8, \ 8, 9, 8, 9, 9, 8, 9, 10, 9, 10, 10, 10, 10, 10, 10, 9, 9, 9, 8, 9, 10,....}

I wanna store the result in the dataset as i am performing further operations on it afterwards. Any help would be appreciated. Thanks.

POSTED BY: Rashmi Dhakad
3 Replies
Posted 2 years ago

Nice Rohit. I was distracted by the whole Dataset thing.

POSTED BY: Eric Rimbey
Posted 2 years ago

Hi Rashmi,

Here is one way to do this in-place modification

data = {-1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 
   1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 1, -1, 1, 0, 0, 0, 0, 0, -1, 0, 
   0, -1, 1, 1, 1};

Make a copy so the original data is preserved.

dataSum = data;
Do[dataSum[[i]] = dataSum[[i - 1]] + dataSum[[i + 1]], {i, 2, Length@data - 1}]

dataSum
(* {-1, -1, 0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6, 7, 8, 9, 8, 8,
8, 9, 8, 9, 9, 8, 9, 10, 9, 10, 10, 10, 10, 10, 10, 9, 9, 9, 8, 9,
10, 11, 1} *}
POSTED BY: Rohit Namjoshi
Posted 2 years ago

Dataset is typically used for data that has more structure than a List. In your case, I see no extra structure, so why are you using Dataset? If there is no compelling reason, I'd suggest you just stick with List.

Your relation, Dataset[i]=Dataset[i-1]+Dataset[i+1], is referencing "forward" (i + 1), and so it's not going to resolve to anything. Why did you only initialize the first element of your list? Also, since Dataset is a built-in symbol, you're not going to be able to redefine it like this.

Looking at your two Lists, I can't figure out what relation you actually intended. But maybe you're trying to do something like this:

start = {-1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 
  1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 1, -1, 1, 0, 0, 0, 0, 0, -1, 0, 
  0, -1, 1};
finish = {-1};
For[i = 2, i < 10, i++, 
 AppendTo[finish, start[[i - 1]] + start[[i + 1]]]]

This doesn't give your desired result, but maybe you can tweak it.

Anyway, some more clarity about what you're trying to do would help. It was a good idea to provide your input and expected output, but unfortunately I just can't understand it.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract