0

I have a matrix A, which has its elements being function of m, say

A[m_]={m, -m*m+2, 5x+m, 3*m+2, -m*m*m, -3+m, m, 5*m*m-4*m, 2*m*m*m}

Now, there is a double For loop, as

For[m = 1, m < 6, m++, 
 For[k = 1, k < 9, k++, 
  If[A[m][[k]] < 0, Print[A[m][[k]], m]; Break[]]]]

This loop is meant to pick elements of the A one by one, if this element is negative, m -> m++ and A is calculated for new m value and so on..

My problem is that for every loop for m, k loop starts from 1. But I want it to start from next element for next cycle.

For example, for m = 1 loop, first negative element (-m*m+2) of A is at second position (k=2) so first o/p of code is 0. Now for next cycle of m, I want k to start values from element which is already picked, so it should start from 3, not from 1. Similarly after m = 2 loop, which has negative element at position 4, I want m = 3 cycle to start from k = 5 and so on... Would appreciate any help on this thanks

user49535
  • 1,225
  • 6
  • 9

1 Answers1

1

I think I'm not entirely clear what you're asking for, but maybe this is close.

Define you list as

a = {# &, 2 - #^2 &, 5 + # &, 3 # + 2 &, -#^3 &, 
       # - 3 &, # &, 5 #^2 - 4 # &, 2 #^3 &};

That is, a is a list of Pure Functions. Then you can Scan (or Map) over those functions while keeping track of m.

m = 1;
Last@Reap@Scan[If[#[m] < 0, Sow[{#[m], m++}]] &, a]

(* {{{-1, 1}, {-1, 2}}} *)

Note that you have to initialize m = 1, but both k and the k-th element of a are now "hidden" by mapping directly over the elements of a.

Explanation

If that's all a bit opaque, have a look at the whole of the matrix you're traversing:

MatrixForm[Table[a[[k]][m], {m, 6}, {k, 9}]

$$ \left( \begin{array}{ccccccccc} 1 & 1 & 6 & 5 & -1 & -2 & 1 & 1 & 2 \\ 2 & -2 & 7 & 8 & -8 & -1 & 2 & 12 & 16 \\ 3 & -7 & 8 & 11 & -27 & 0 & 3 & 33 & 54 \\ 4 & -14 & 9 & 14 & -64 & 1 & 4 & 64 & 128 \\ 5 & -23 & 10 & 17 & -125 & 2 & 5 & 105 & 250 \\ 6 & -34 & 11 & 20 & -216 & 3 & 6 & 156 & 432 \\ \end{array} \right) $$

and include a bit more commentary in the above function:

m = 1;
Reap@(
  With[{fun = #},
     Print[ToString[{m, fun[m]}]];
     If[fun[m] < 0, Sow[{fun[m], m++}]]
     ] & /@ a)

(* {1, 1}    
   {1, 1}    
   {1, 6}    
   {1, 5}    
   {1, -1}    
   {2, -1}    
   {3, 3}    
   {3, 33}    
   {3, 54}

   {{Null, Null, Null, Null, {-1, 1}, {-1, 2}, Null, Null, Null}, 
   {{{-1, 1}, {-1, 2}}}} *)

So you can see how the function moves across the top row of the matrix -- 1, 1, 6, 5 -- but nothing happens (all those Nulls) until it hits the first -1 in the fifth position. The function then Sows the value of that function at that m-value -- a[[5]][1] == -1 -- and the m-value, which it then increments. Then it's on row 2 in the sixth position, where there's another -1, which also gets Sown, along with m == 2. m is incremented again, and reaches the end of a with m == 3 as there are no more negatives. Those Sown values are collected by the Reap at the start.

aardvark2012
  • 5,424
  • 1
  • 11
  • 22