cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Abby_Collins14
Level II

using for loop to find minimum values and saving results in a list

I have three lists of numbers:

 

a= {2,3,4,5,6}

b = {4,5,7,2,1}

c= {9,6,4,6,2}

I would like to take the minimum of each index from the 3 lists and turn that into a list, say d. so d = {2,3,4,2,1}.

the loop i have started is:

 

minsl = {};

for(i = 1, i = 6, i++,

mins = {a[i], b[i], c[i]};


minval = minimum(mins);


minsl=insert into(minsl,minval,i);
If(n items(minsl)==5, break()); // this line was added to stop the loop because it gets stuck
);

 

the output is minsl = {1,1,1,1,1} i.e. is gets stuck storing the last entry in the list only (and foes on forever without the break line), when it should be {2,3,4,2,1}

 

Any suggestions how to fix?? Thanks!!

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: using for loop to find minimum values and saving results in a list

Your For() loop of 

For( i = 1, i = 5, i++, 

needs to be

For( i = 1, i <= 5, i++, 

Then it seems to work the way you want it to

names default to here( 1 );

a = {2, 3, 4, 5, 6};
b = {4, 5, 7, 2, 1};
c = {9, 6, 4, 6, 2};

minsl = {};

For( i = 1, i <= 5, i++, 
	mins = a[i] || b[i] || c[i];
	minval = Minimum( mins );
	Insert Into( minsl, minval );
);
Jim

View solution in original post

jthi
Super User

Re: using for loop to find minimum values and saving results in a list

You can also use matrices + V Min() for this (remember to comment to code to make it easier to understand if needed)

Names Default To Here(1);

a = {2, 3, 4, 5, 6};
b = {4, 5, 7, 2, 1};
c = {9, 6, 4, 6, 2};

m = Matrix(a)` |/ Matrix(b)` |/ Matrix(c)`;
d = As List(V Min(m)`); // {2, 3, 4, 2, 1}
-Jarmo

View solution in original post

7 REPLIES 7
txnelson
Super User

Re: using for loop to find minimum values and saving results in a list

Here is how I would approach the issue

names default to here(1);
a = {2, 3, 4, 5, 6};
b = {4, 5, 7, 2, 1};
c = {9, 6, 4, 6, 2};

a1= sortlist(a);
remove from(a1,3,n=length(a)-2);
b1= sortlist(b);
remove from(b1,3,n=length(b)-2);
c1= sortlist(c);
remove from(c1,3,n=length(c)-2);

d=a1||b1||c1;

Also, please use the <JSL> icon at the top of the Preview window when entering script code.  It allows for the reader to more easily read the code.

Jim
Abby_Collins14
Level II

Re: using for loop to find minimum values and saving results in a list

thanks! this doesn't seem to preserve the order though. Essentially, I need to pull the minimum value out of the first index of the 3 lists and then store that in a list (preserving order) 

 

a = {2, 3, 4, 5, 6};
b = {4, 5, 7, 2, 1};
c = {9, 6, 4, 6, 2};

minsl = {};

for(i = 1, i = 5, i++,

mins = {a[i],b[i],c[i]};
minval = minimum(mins);
insert into (minsl,minval,i);
If(n items(minsl)==5, break());
);

if you run the above code ignoring the actual loop and do i=1, and then run just the inside of the loop and then put i=2 and run the inside of the loop it works (you'll see minsl = {2,3} )but for some reason the loop doesn't cycle properly and breaks

Abby_Collins14
Level II

Re: using for loop to find minimum values and saving results in a list

a = {2, 3, 4, 5, 6};
b = {4, 5, 7, 2, 1};
c = {9, 6, 4, 6, 2};

minsl = {};

i = 1;

mins = {a[i],b[i],c[i]};
minval = minimum(mins);
insert into (minsl,minval,i);

i=2;

mins = {a[i],b[i],c[i]};
minval = minimum(mins);
insert into (minsl,minval,i);

i = 3;

mins = {a[i],b[i],c[i]};
minval = minimum(mins);
insert into (minsl,minval,i);

i = 4;

mins = {a[i],b[i],c[i]};
minval = minimum(mins);
insert into (minsl,minval,i);


minsl

i.e. the above works, but I would like to get a loop working for when I have lists of much longer lengths 

txnelson
Super User

Re: using for loop to find minimum values and saving results in a list

Your For() loop of 

For( i = 1, i = 5, i++, 

needs to be

For( i = 1, i <= 5, i++, 

Then it seems to work the way you want it to

names default to here( 1 );

a = {2, 3, 4, 5, 6};
b = {4, 5, 7, 2, 1};
c = {9, 6, 4, 6, 2};

minsl = {};

For( i = 1, i <= 5, i++, 
	mins = a[i] || b[i] || c[i];
	minval = Minimum( mins );
	Insert Into( minsl, minval );
);
Jim
Abby_Collins14
Level II

Re: using for loop to find minimum values and saving results in a list

thanks!!

jthi
Super User

Re: using for loop to find minimum values and saving results in a list

You can also use matrices + V Min() for this (remember to comment to code to make it easier to understand if needed)

Names Default To Here(1);

a = {2, 3, 4, 5, 6};
b = {4, 5, 7, 2, 1};
c = {9, 6, 4, 6, 2};

m = Matrix(a)` |/ Matrix(b)` |/ Matrix(c)`;
d = As List(V Min(m)`); // {2, 3, 4, 2, 1}
-Jarmo
Abby_Collins14
Level II

Re: using for loop to find minimum values and saving results in a list

didn't think of this, good idea-thanks!