Message Boards Message Boards

0
|
7648 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Can list be a loop counter?

Posted 10 years ago

Hello, Community! A problem: there is a list (counted before, not to be count in parallel with next step), now it's a base for next step of computation. I need to take one element of a list (its place is important) and use it to compute a sublist of the first list. Then exclude this sublist from the first list, and take new element (of a new shorter list), compute new sublist... Repeate a loop till the first list becomes {}. If a loop counter is a number - all is OK: increments ++,-- work good. But when i try list as a loop counter and decrease it by Complement - have a problem. First i did it by hands - in one notebook count sublist, in second - exclude it from first list, take this result to the first notebook and so on. Example - see attached file

Attachments:
6 Replies

Very well, it works! Thanks a lot!!!

Hello Alexey,

I changed your notebook a bit, have a look at this piece of code:

ClearAll["Global`*"]

a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
For[i = 2, i <= Length[a], i++,
 b = Select[a, # < 2 a[[i]] || # > 3 a[[i]] &];
 Print[i, ",", b];
 Print["####  ", a];
 a = Complement[a, b];
 Print["****  ", a]]

Basically you need to seperate your commands with a semicolon (;). In general one should try to avoid For-loops and this infamous Print command; better would be a more functional style and e.g. a Reap - Sow constuct for returning values as in my example above.

Regards -- Henrik

POSTED BY: Henrik Schachner

Hello, Otto (thanx for your interest) - i change a description of a problem, hope now it's more understandable.

Hello, Henrik! Your questions help me to understand what i want (thanx) - need to change the base list inside the loop many times with some (not changing) operations. Another attempt - see attached file

Attachments:

Hi Ampo,

I am not sure: Could this be a minimal example of what you are intending?

ClearAll["Global`*"]

a = Range[10];
result = Reap[While[Length[a] > 1,
     (* calculation of a specific index, e.g.: *)
     fIndx = 1 + Ceiling[(Length[a] - 1) Abs[Sin[Length[a]]]];
     (* calculation of depending sublist: *)
     subA = RandomSample[Range[fIndx], 2];
     (* removing elements of sublist from list: *)
     a = Complement[a, a[[subA]]];
     Sow[a]]][[2, 1]];

result // TableForm

Regards -- Henrik

POSTED BY: Henrik Schachner

Hello Podchufarov,

I would like to contribute, but I find the description hard to follow. Could you try to clarify? For example, you say that you take an element of a list (at random?) and use it to compute (by calling an existing function) a sublist of a list (of a different list or of the same one?).

Also, could you be more specific about the sizes of the lists? One could come up with something that works fine for a list of 10 elements, but that crashes on a list of 80K elements.

Thanks,

OL.

POSTED BY: Otto Linsuain
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